[英]How do I return to the beginning of a while loop?
我只參加了一個月的編碼課程,所以如果我使用了一些錯誤/古怪的結構,我真的很抱歉。
我正在嘗試使用兩個隨機數制作加法游戲; r1確定加法問題中的項數,而r2為每個項分配一個值。 掃描器檢查用戶的輸入,並根據游戲給出分數或計算失敗的嘗試。
問題是第一次迭代運行良好,但它從不顯示新問題,即使尚未滿足主 while 循環的條件(3 個錯誤答案)。
你能給我一些指點嗎? 謝謝!
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
public class AddingGame {
Random rand1, rand2;
int points = 0, wrong = 0, sum;
String str = "";
Scanner sc;
ArrayList <Integer> mem;
public AddingGame() {
sc = new Scanner(System.in);
rand1 = new Random();
rand2 = new Random();
int r1, r2, i;
mem = new ArrayList <> ();
System.out.println("Input any integer to receive a problem. Answer the problem correctly to proceed. Each correct answer yields a point. After three incorrect attempts, the game ends.");
while (wrong <= 3) {
mem.removeAll(mem);
sum = 0;
i = 0;
str = "";
r1 = rand1.nextInt(5) + 2; // determines the number of terms of a problem
while (mem.size() <= (r1 - 1)) {
r2 = rand2.nextInt(50) + 1; // determines the value of each term
mem.add(r2);
sum += r2;
if (i == (r1 - 1)) {
str = str + mem.get(i) + " = ?";
} else {
str = str + mem.get(i) + " + ";
}
i++;
}
System.out.println(str);
correct();
while (sc.nextInt() != sum) {
System.out.println("Try again!");
System.out.println(str);
wrong++;
if (wrong == 3) {
System.out.println("You're out of attempts!");
break;
}
correct();
}
}
}
private void correct() {
if (sc.nextInt() == sum) {
points++;
if (points != 1) {
System.out.println("Correct! You now have " + points + " points!" );
} else {
System.out.println("Correct! You now have " + points + " point!" );
}
}
}
}
我想你可能想看看這個游戲的更簡單的實現,因為我注意到你正在努力弄清楚你需要哪些變量,哪些數據結構會有所幫助等等!
希望這能幫助你更好地理解如何用 Java 構建你的程序:
import java.util.Random;
import java.util.Scanner;
public class AddingGame {
Random rand = new Random(); // no reason to have more than one instance of Random
int points; // ints are automatically initialized to zero
Scanner sc = new Scanner(System.in);
public AddingGame() {
System.out.println("Input any integer to receive a problem. Answer the problem correctly to proceed. Each correct answer yields a point. After three incorrect attempts, the game ends.");
mainLoop:
do { // keep playing until player gives up
int sum = 0;
String question = "";
int termsCount = rand.nextInt(5) + 2; // determines the number of terms of a problem
for (int i = 0; i < termsCount; i++) {
int term = rand.nextInt(50) + 1; // determines the value of each term
sum += term;
if (i == termsCount - 1) { // this is the last term
question += term + " = ? ";
} else {
question += term + " + ";
}
}
System.out.print(question);
int wrong = 0;
while (!checkAnswerIsCorrect(sum)) {
wrong++;
if (wrong == 3) {
System.out.println("You're out of attempts!");
break mainLoop; // get out of mainLoop, not just the current one!
}
System.out.println("Try again!");
System.out.print(question);
}
} while (shouldContinuePlaying());
System.out.println("Total Points: " + points);
System.out.println("GAME OVER!");
}
private boolean checkAnswerIsCorrect(int sum) {
if (sc.nextInt() == sum) {
points++;
System.out.println("Correct! You now have " + points + " point" +
(points == 1 ? "" : "s") + "!");
return true;
}
return false;
}
private boolean shouldContinuePlaying() {
System.out.print("Continue playing? ");
return sc.next().equals("yes");
}
public static void main(String[] args) {
new AddingGame();
}
}
我建議您在理解我的更改后將循環拆分為單獨的方法......這將使您更容易“專注於”游戲的單個部分,而不必在閱讀時將所有內容都記在腦海中代碼...我將大多數字段移至局部變量,因為在任何給定時間,范圍內只有少量事物(您可以“看到”的變量)總是更可取的 - 再次幫助程序員理解哪些值是重要的,並且可以/應該使用。
祝你好運,並希望你有一個偉大的程序員職業生涯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.