所以我一直在研究这个Java程序,计算机询问用户是否想知道数字的平方。如果用户的答案是“Y”或“y”,则计算机向用户询问他想要知道的数字,并打印数字的平方。然后计算机询问用户他是否想知道另一个号码的平方。如果答案是'Y'或'y',计算机将获取一个数字并打印正方形。再次询问用户是否想知道另一个号码的平方。像这样,它继续下去。
我还为用户的答案是否定的,或既不是正面也不是负面的情况编写代码。通过这个,我的意思是用户的答案是'N'或'n'。这个程序在某个地方遇到问题,并且不能按照我想要的方式从A到Z工作。看,当我运行代码时,计算机会问我是否想知道数字的平方,然后输入'y'。然后我被问到我想知道的数字。我输入数字,计算机打印出它的正方形。然后我被问到我是否想知道另一个数字的平方。我输入'y'。然后我被问到我想知道的数字,我输入一个数字。计算机打印数字的平方。然后程序询问我是否想要知道另一个数字的平方并且刚刚结束,而它应该采取我的答案。我已经工作了近16到17个小时试图找到这个bug,但我做不到。你能不能测试代码并告诉我哪里出错了?感谢。
这是代码:
package looppracticea;
import java.util.Scanner;
public class LoopPracticeA {
public static void main(String[] args) {
Scanner geek = new Scanner(System.in);
Scanner geek2 = new Scanner(System.in);
long taken_number, taken_number2;
String answer, answer2, answer_not_recognized, if_answer_is_no;
answer_not_recognized = "You didn't enter any of the recognized answers.";
if_answer_is_no = "'Kay. Whatever.";
System.out.println("Do you want to know the square of a number? (Y/N)");
answer = geek.nextLine();
if (answer.equals("Y") || answer.equals("y")) {
System.out.println("The number you want to know the square of is:");
taken_number = geek.nextLong();
System.out.println("That number squared is " + taken_number * taken_number + ".");
System.out.println("Do you want to know the square of another number? (Y/N)");
answer2 = geek2.nextLine();
switch (answer2) {
case "N":
System.out.println(if_answer_is_no);
break;
case "n":
System.out.println(if_answer_is_no);
break;
case "Y":
while (answer2.equals("Y")) {
System.out.println("The number you want to know the square of is:");
taken_number2 = geek2.nextLong();
System.out.println("That number squared is " + taken_number2 * taken_number2 + ".");
System.out.println("Do you want to know the square of another number? (Y/N)");
answer2 = geek2.nextLine();
}
break;
case "y":
while (answer2.equals("y")) {
System.out.println("The number you want to know the square of is:");
taken_number2 = geek2.nextLong();
System.out.println("That number squared is " + taken_number2 * taken_number2 + ".");
System.out.println("Do you want to know the square of another number? (Y/N)");
answer2 = geek2.nextLine();
}
break;
default:
System.out.println(answer_not_recognized);
break;
}
} else if (answer.equals("N") || answer.equals("n")) {
System.out.println(if_answer_is_no);
} else {
System.out.println();
System.out.println(answer_not_recognized);
}
}
}
答案 0 :(得分:0)
以下是您的主要内容的固定内容:
Scanner geek = new Scanner(System.in);
long taken_number;
String answer;
String answer_not_recognized = "You didn't enter any of the recognized answers.";
String if_answer_is_no = "'Kay. Whatever.";
boolean firstTime = true;
while(true)
{
if(firstTime)
System.out.println("Do you want to know the square of a number? (Y/N)");
else
System.out.println("Do you want to know the square of another number? (Y/N)");
answer = geek.nextLine();
if (answer.equalsIgnoreCase("y"))
{
System.out.println("The number you want to know the square of is:");
taken_number = geek.nextLong();
System.out.println("That number squared is " + taken_number * taken_number + ".");
answer = geek.nextLine();
}
else if (answer.equalsIgnoreCase("n")) {
System.out.println(if_answer_is_no);
break;
} else {
System.out.println();
System.out.println(answer_not_recognized);
break;
}
if(firstTime)
firstTime = false;
}
问题是您必须两次读取该行才能到达您想要的行。
第一个nextLine
将读取输入
我还通过以下方式减少了代码:
Scanner
,因为这足够了equalsIgnoreCase
代替2 equals
answer
和taken_number
变量,因为这足够了nextLine()
2次,因为第一个是输入