这是我到目前为止所得到的但是由于某种原因,程序声明操作完成而不允许用户输入1或2继续或不继续。提前谢谢..
我的代码:
import java.util.Random;
public final class Kidwell_Lab09 {
public static Random generator = new Random();
public static void main(String[] args) {
int x;
int[] randomNumbers = new int[20];
do
{
Random generator = new Random();
for (int i = 0; i < randomNumbers.length; i++){
int n = generator.nextInt(10)+1;
randomNumbers[i] = n;
}
for (int i = 0; i < randomNumbers.length; i++) {
System.out.println("Number " + i + " : " + randomNumbers[i]);
}
System.out.print("Do you wish to restart the program, Enter 1 for YES, 2 for NO: ");
x = generator.nextInt();
}
while ( x == 1 );
}
}
答案 0 :(得分:0)
您应该从System.in而不是从generator.nextInt()读取值。
这是更正后的代码。
import java.util.Random;
public final class Kidwell_Lab09 {
public static Random generator = new Random();
public static void main(String[] args) {
int x;
int[] randomNumbers = new int[20];
Scanner inputReader = new Scanner(System.in);
do {
Random generator = new Random();
for (int i = 0; i < randomNumbers.length; i++) {
int n = generator.nextInt(10) + 1;
randomNumbers[i] = n;
}
for (int i = 0; i < randomNumbers.length; i++) {
System.out.println("Number " + i + " : " + randomNumbers[i]);
}
System.out.print("Do you wish to restart the program, Enter 1 for YES, 2 for NO: ");
x = inputReader.nextInt();
} while (x == 1);
}
}