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