提示用户继续不工作,java
Prompting user to continue not working, java
我需要帮助。我想询问用户是否想再试一次,但我的代码似乎有问题,因为它不起作用。
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"));
}
}
当我 运行 它时,它会存储用户的答案,但程序不会重复。我该如何解决这个问题?
在 n2 = keyboard.nextInt();
之后移动 keyboard.nextLine();
以接受并忽略输入流中因调用 nextInt()
而留下的 悬空换行符 。
当我 运行 它存储用户的答案 - 尝试打印它存储在 answer
字段中的内容然后你会看到问题.
改变keyboard.nextLine();
的位置。
keyboard.nextLine();
answer = keyboard.nextLine();
在您的代码中,答案是获取下一行(即 enter
),当您获取 n2 的值并按回车键时,它就会出现。
您可以通过执行以下代码来测试您的代码
System.out.println("Enter yes to continue or any other key to end");
answer = keyboard.nextLine();
System.out.println("Answer : " + answer);
System.out.println(keyboard.nextLine());
Scanner keyboard = new Scanner(System.in);
int n1, n2, sum;
String answer = "Yes";
while (answer.equals("Yes"))
{
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();
}
我需要帮助。我想询问用户是否想再试一次,但我的代码似乎有问题,因为它不起作用。
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"));
}
}
当我 运行 它时,它会存储用户的答案,但程序不会重复。我该如何解决这个问题?
在 n2 = keyboard.nextInt();
之后移动 keyboard.nextLine();
以接受并忽略输入流中因调用 nextInt()
而留下的 悬空换行符 。
当我 运行 它存储用户的答案 - 尝试打印它存储在 answer
字段中的内容然后你会看到问题.
改变keyboard.nextLine();
的位置。
keyboard.nextLine();
answer = keyboard.nextLine();
在您的代码中,答案是获取下一行(即 enter
),当您获取 n2 的值并按回车键时,它就会出现。
您可以通过执行以下代码来测试您的代码
System.out.println("Enter yes to continue or any other key to end");
answer = keyboard.nextLine();
System.out.println("Answer : " + answer);
System.out.println(keyboard.nextLine());
Scanner keyboard = new Scanner(System.in);
int n1, n2, sum;
String answer = "Yes";
while (answer.equals("Yes"))
{
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();
}