我需要帮助。我想询问用户是否想再试一次,但我的代码似乎有问题,因为它不起作用。
public class TotoAzul
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int n1, n2, sum;
String answer;
do {
System.out.println("Enter number 1: ");
n1 = keyboard.nextInt();
System.out.println("Enter number 2: ");
n2 = keyboard.nextInt();
sum = n1 + n2;
System.out.println("Number 1\t" + "Number 2\t" + "Sum");
System.out.println("__________________________________");
System.out.println(n1 + "\t\t" + n2 + "\t\t" + sum);
System.out.println("Enter yes to continue or any other key to end");
answer = keyboard.nextLine();
keyboard.nextLine();
}
while(answer.equalsIgnoreCase("YES"));
}
}
当我运行它时,它会存储用户的答案,但程序不会重复。我该如何解决这个问题?
最佳答案
将 keyboard.nextLine();
移至 n2 = Keyboard.nextInt();
之后,以接受并忽略中的悬空换行符通过调用 nextInt()
留下的输入流。
当我运行它时,它会存储用户的答案 - 尝试打印它存储在 answer
字段中的内容,然后您就会看到问题。
关于java - 提示用户继续不工作,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29840150/