嗨我正在尝试制作一个程序,当你运行的时候就像你和一个机器人交谈,这个机器人会询问一些关于你自己的问题,但是我已经包含了一个部分,它会问你是否要继续然后我会卡住如果用户输入“否”,如何使程序继续运行或关闭。到目前为止,这是我的所有代码:
/********************
*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 :(得分:1)
您可以使用
System.exit(0)
匹配'否'
答案 1 :(得分:0)
您可以将{{1}}放在if语句中以结束您的程序
答案 2 :(得分:0)
您应该使用if
声明检查该值:
if(answer.equals("NO")){
return;
}
如果用户键入“NO”,则退出程序,否则继续。
答案 3 :(得分:0)
让你的代码在循环中运行
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String input="";
While(!(input=scan.nextLine()).equals("NO")){
// your code goes here
System.out.println("input not NO");
}
}