所以我一直在研究這個Java程序,在這個程序中,計算機詢問用戶他是否想知道數字的平方。如果用戶的答案是'Y'或'y',計算機會向用戶詢問他想要的數字,並且想知道數字的平方,並打印數字的平方。然後計算機詢問用戶他是否想知道另一個數字的正方形。如果答案是'Y'或'y',則計算機會記錄一個數字並打印出正方形。再次詢問用戶他是否想知道另一個數字的平方。像這樣繼續。無法評估用戶輸入
我也寫過代碼的情況下,用戶的答案是否定的,或既不積極也不消極。通過這個,我的意思是用戶的答案是'N'或'n'的情況。這個程序在某個地方遇到問題,並且不按照我希望的方式從A到Z工作。看,當我運行代碼時,計算機問我是否想知道數字的平方,然後輸入'y'。然後我被問到我想知道的平方數。我輸入數字,電腦打印出它的正方形。然後我被問到是否想知道另一個數字的平方。我輸入'y'。然後我被問到我想知道廣場的數字,然後輸入一個數字。計算機打印數字的平方。然後該程序詢問我是否想知道另一個數字的正方形,並且只是結束,而本來應該回答我的答案。我已經工作了將近16到17個小時,試圖找到這個錯誤,但是我不能。你可以請測試代碼並告訴我哪裏出了錯?謝謝。
這是代碼:
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);
}
}
}
你沒有張貼此相同的問題在幾分鐘前? – PakkuDon
爲什麼你反覆詢問同一個問題? –
因爲之前的版本是不健全的。請您回答這個問題嗎? – user3590394