我正在尝试制作一个添加游戏,但我随机生成的数字要添加的并不是随机的,即使我每次都指定它们是随机的。有人帮忙。我还在处理我的项目,但我仍然坚持这一部分。到目前为止我的代码:
package addgame;
import java.util.Random;
import java.util.Scanner;
public class add {
private static Scanner console;
public static void main(String[] args) {
System.out.println(
"We are going to play an adding game. Type your answer.");
equation();
}
public static void Answer(Scanner console) {
}
public static void equation() {
int tries = 0;
while (tries == 0) {
Random rand = new Random();
int nums = rand.nextInt(25) + 6;
int totalnums = rand.nextInt(4) + 1;
int sumAns = 0;
for (int i = 1; i <= totalnums + 1; i++) {
System.out.print(nums + "+ ");
sumAns += nums;
}
System.out.print("= ");
console = new Scanner(System.in);
int ans = console.nextInt();
System.out.println(sumAns);
}
}
}
答案 0 :(得分:0)
您必须将随机数发放器放在for-loop
:
private static Scanner console;
public static void main(String[] args) {
System.out.println(
"We are going to play an adding game. Type your answer.");
equation();
}
public static void Answer(Scanner console) {
}
public static void equation() {
int tries = 0;
while (tries == 0) {
Random rand = new Random();
int nums = 0;
int totalnums = rand.nextInt(4) + 1;
int sumAns = 0;
for (int i = 1; i <= totalnums + 1; i++) {
nums = rand.nextInt(25) + 6; //Changed
System.out.print(nums + "+ ");
sumAns += nums;
}
System.out.print("= ");
console = new Scanner(System.in);
int ans = console.nextInt();
System.out.println(sumAns);
}
}
}