我如何 return 到 while 循环的开头?
How do I return to the beginning of a while loop?
我只学习编码 类 一个月,所以如果我使用了一些 wrong/wacky 结构,我真的很抱歉。
我正在尝试使用两个随机数制作一个加法游戏; 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();
}
}
我建议您在了解我的更改后将循环拆分为单独的方法...这将使您更容易“专注于”游戏的单个部分,而不必将所有内容都保留在您的当你阅读代码时,请注意...我将大部分字段移至局部变量,因为在任何给定时间范围内只包含少量内容(你可以“看到”的变量)总是更可取 - 再次,以帮助程序员理解什么值很重要并 can/should 被使用。
祝你好运,希望你有一个伟大的程序员职业。
我只学习编码 类 一个月,所以如果我使用了一些 wrong/wacky 结构,我真的很抱歉。
我正在尝试使用两个随机数制作一个加法游戏; 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();
}
}
我建议您在了解我的更改后将循环拆分为单独的方法...这将使您更容易“专注于”游戏的单个部分,而不必将所有内容都保留在您的当你阅读代码时,请注意...我将大部分字段移至局部变量,因为在任何给定时间范围内只包含少量内容(你可以“看到”的变量)总是更可取 - 再次,以帮助程序员理解什么值很重要并 can/should 被使用。
祝你好运,希望你有一个伟大的程序员职业。