[英]do while loop with a Yes/No user prompt
我的代碼有問題。 該代碼是找到一個數字的階乘,然后詢問您是否要再次運行該程序,假設再次運行然后退出。 但是,當我輸入Y
重新啟動程序時,它會中斷並且不會重新啟動,而當我輸入N
退出時,它不會退出程序。
private static Object Cont;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Greetings
System.out.println("Welcome to my factorial program! ");
System.out.println("Please choose from the following: ");
//Menu
System.out.println("1. Run Program");
System.out.println("2. Exit Program");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("This program will determine the factorial value of positive integers.");
do {
System.out.println("The starting number is 1.");
System.out.println("Please enter an ending integer value:");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println(i + "! = " + fact(i));//call to function
}
System.out.println("Run factorial program again? (Y for Yes, N for No): ");
String Cont = scanner.next();
if (Cont.equals("N")) {
break;
}
} while (Cont.equals("Y"));// do while loop
break;
//Menu Exit
case 2:
System.out.println("Thank you for using the program.");
System.out.println("Goodbye");
default:
System.exit(1); // remebered from last week to set this to one
System.out.println("Goodbye");
break;
}
}//Factorial Math
static long fact(int x) {
long f = 1;
for (int i = 1; i <= x; i++) {
f = f * i;
}
return f;
} //End Main Method
我錯過了什么或做錯了什么?
在do
循環之前,您需要額外的break
並聲明 Cont:
//private static Object Cont; This is is not declared on the right location, we'll declare it later
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Greetings
System.out.println("Welcome to my factorial program! ");
System.out.println("Please choose from the following: ");
//Menu
System.out.println("1. Run Program");
System.out.println("2. Exit Program");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("This program will determine the factorial value of positive integers.");
String Cont = null; // Cont must be declared here
do {
System.out.println("The starting number is 1.");
System.out.println("Please enter an ending integer value:");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println(i + "! = " + fact(i));//call to function
}
System.out.println("Run factorial program again? (Y for Yes, N for No): ");
Cont = scanner.next();
if (Cont.equals("N")) {
break;
}
} while (Cont.equals("Y"));// do while loop
break;
//Menu Exit
case 2:
System.out.println("Thank you for using the program.");
System.out.println("Goodbye");
break; // requires this additional break
default:
System.exit(1); // remembered from last week to set this to one
System.out.println("Goodbye");
break;
}
}//Factorial Math
static long fact(int x) {
long f = 1;
for (int i = 1; i <= x; i++) {
f = f * i;
}
return f;
} //End Main Method
你在這里有幾個問題。
第一個問題是您聲明了兩個不同的Cont
變量。 第一個是static
字段。 第二個是在循環體中聲明的局部變量。
我不知道你為什么聲明靜態字段,但我想你這樣做是因為} while (Cont.equals("Y"));
沒有它就無法編譯。 (這是因為在循環中聲明的Cont
變量不在循環體之外的范圍內。)不幸的是,這不是正確的解決方案。 因為,您現在擁有分配給一個變量並測試另一個變量的代碼。 自然,這是行不通的。
在我看來,正確的解決方案是去掉static
字段和循環體中的聲明。 在循環開始之前為Cont
添加一個聲明。 (它不應該有初始化)。 最后,在循環中,您只需要讀取(使用掃描儀)並將字符串分配給Cont
以便您可以在循環條件中進行測試。
第二個問題是您在那里進行了冗余測試。 如果您要測試以查看是否需要繼續使用} while (Cont.equals("Y"));
你不也需要測試,如果Cont
為"N"
和break
。
相關地, equals("Y")
與 not equals("N")
。 (考慮"Hello"
... 或"n"
。它們既不是"Y"
也不是"N"
。)所以如果你真的想在用戶輸入N
時停止循環,那么循環終止條件應該是:
} while (!Cont.equals("N")); // keep looping while not 'N'
最后,還有一些與風格相關的重要問題。
聲明一個static
字段通常是一個錯誤。
當您應該使用局部變量時使用字段是錯誤的。 僅與方法的單次執行相關的狀態應使用局部變量表示1 。
變量以大寫字母開頭是一個主要的文體錯誤。 Cont
應該是cont
。
如果您曾在注重風格的專業Java 開發團隊工作,您將很難忽視 Java 標識符約定。 並且(IMO)你的老師應該為這個錯誤停靠樣式標記。
1 - 原因: 1)它使方法更難閱讀,因為變量聲明遠離它的使用。 2) 它通常使代碼不可重入。 3)它使您容易在方法之間產生不必要的耦合; 例如,如果兩個方法意外共享同一個“聲明為字段的局部變量”。 4) 在許多情況下,它使用更多的內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.