java退出代码怎么打_在java中,如果用户输入“YES”,我该如何继续程序并在用户输入“NO”时关闭它?...

嗨即时试图做一个程序,当你运行的行为就像你跟一个机器人谈话,会问你一些关于你自己的问题,但我已经包含了一个部分,如果你想继续,但是我会被卡住如果用户输入“否”,如何使程序继续运行或关闭。这是我所有的代码至今:在java中,如果用户输入“YES”,我该如何继续程序并在用户输入“NO”时关闭它?

/********************

*Husnain Sheraz

*06/08/17

*To test knowledge

*Home

********************/

import java.util.concurrent.TimeUnit;

import java.util.Scanner;

public class reviseJava{

public static void main(String[] args)

throws InterruptedException {

//Introduction just saying my name.

printWithDelays("Hello my name is HUS9.EXE", TimeUnit.MILLISECONDS, 100);

TimeUnit.SECONDS.sleep(2);

//Asking user's name and printing it.

//start a new line

System.out.println("");

Scanner scan = new Scanner(System.in);

//A prompt for the user to enter his name.

printWithDelays("What's your name?", TimeUnit.MILLISECONDS, 100);

//reads the next letters (string) the user presses before hitting enter.

String usersName = scan.nextLine();

//printing out the word hello and what ever the user inputted as his/hers name.

printWithDelays("Hello " + usersName, TimeUnit.MILLISECONDS, 100);

TimeUnit.SECONDS.sleep(1);

printWithDelays(" i'm going to ask you some basic questions about yourself. If you would like to continue please type 'YES' if you would not like to continue please type 'NO'.", TimeUnit.MILLISECONDS, 100);

printWithDelays("Would you like to continue?", TimeUnit.MILLISECONDS, 100);

String answer = scan.nextLine();

}

public static void printWithDelays(String data, TimeUnit unit, long delay)

throws InterruptedException {

for (char ch:data.toCharArray()) {

System.out.print(ch);

unit.sleep(delay);

}

}

}

+0

你的代码中需要研究的java.util.Scanner中的类。 –

+0

@DevelopingDeveloper实际上,这个问题询问如何获得输入,这个是关于如何根据变量的值退出,因为输入已经被接收。 –

+1

[终​​止Java程序]的可能的重复(https://stackoverflow.com/questions/22452930/terminating-a-java-program) –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值