嗨,我有这个程序:
import java.util.Scanner;
public class HowAreYou {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input;
System.out.println("How are you?");
input = in.nextLine();
if (input.equals("I'm doing good!")) {
System.out.print("That's great to hear!");
} else if (input.equals("I'm not doing too well...")) {
System.out.print("Aw I'm sorry to hear that");
} else {
System.out.print("Sorry I didn't catch that are you doing good or bad?");
input = in.nextLine();
if (input.equals("good")) {
System.out.print("That's great to hear!");
} else if (input.equals("bad")) {
System.out.print("Aw I'm sorry to hear that");
}
}
}
}
对于前两个响应,它工作正常,如果输入的内容不是它,它会显示“对不起,我没发现您做的好还是不好?” 正确,但是我希望它在打印后再次得到响应。在它说“抱歉,我没注意到你做的好还是不好?”之后的那一刻。它不允许您输入其他任何内容。
我认为您面临的问题是,在"Sorry I didn't catch that are you doing good or bad?"
您打enter key
了一条消息以给出响应后程序便终止了。发生这种情况是因为使用了input.nextLine
它,但它与任何内容都不匹配,因此您的程序退出了。
你应该更换
System.out.print("Sorry I didn't catch that are you doing good or bad?");
和
System.out.println("Sorry I didn't catch that are you doing good or bad?");
这样您就可以在实际输入之前进入下一行。希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句