【发布时间】:2017-08-07 16:25:28
【问题描述】:
您好,我正在尝试制作一个程序,当您运行该程序时,它就像您与机器人交谈,机器人会问一些关于您自己的问题,但我已经包含了一个部分,它会询问您是否愿意继续,但后来我被卡住了如果用户输入“否”,如何使程序继续运行或关闭。到目前为止,这是我所有的代码:
/********************
*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);
}
}
}
【问题讨论】:
-
你需要研究 Java.Util.Scanner 类。
-
@DevelopingDeveloper 不是真的,这个问题是问如何获取输入,这个问题是关于如何根据变量的值退出,因为已经收到了输入。
标签: java