以下需要采用主要方法:
到目前为止,这是我的代码,收到一些错误:
Kidwell_Lab09.java:29: error: incompatible types: int[] cannot be converted to int randomNumbers = new int[SIZE];
Kidwell_Lab09.java:32: error: array required, but int found System.out.println("Number " + i + " : " + randomNumbers[i]);
Kidwell_Lab09.java:39: error: cannot find symbol x = inputReader.nextInt();
symbol: variable x location: class Kidwell_Lab09
Kidwell_Lab09.java:40: error: cannot find symbol } while (x == 1);
symbol: variable x location: class Kidwell_Lab09 4 errors
在这一点上,我感到有些失落,提前感谢:
import java.util.Scanner;
import java.util.Random;
public final class {
public static Random generator = new Random();
public int createNum(int[] randomNumbers, int SIZE, int n, int i) {
int x;
SIZE = 20;
randomNumbers = new int[SIZE];
Random generator = new Random();
for (i = 0; i < SIZE; i++) {
n = generator.nextInt(10) + 1;
randomNumbers[i] = n;
}
return n;
}
public void print(int i, int randomNumbers, int SIZE){
SIZE = 20;
randomNumbers = new int[SIZE];
for (i = 0; i < SIZE; i++) {
System.out.println("Number " + i + " : " + randomNumbers[i]);
}
}
public static void main(String[] args){
do{
Scanner inputReader = new Scanner(System.in);
System.out.print("Do you wish to restart the program, Enter 1 for YES, 2 for NO: ");
x = inputReader.nextInt();
} while (x == 1);
}
}
答案 0 :(得分:2)
您的说明对我来说似乎很清楚,让我们从method1
开始;您将在范围的开头和范围的结尾处传递,并在这两个值之间返回一个随机值。你有一个Random
,所以让我们使用它。像,
private static Random generator = new Random();
public static int method1(int start, int end) {
return generator.nextInt(1 + end - start) + start;
}
接下来,您需要将数组及其长度传递给method2
(用于显示数组)。这是一种这样做的方式(我希望你的导师预计会在这里使用for
循环,但我会把它留作练习)
public static void method2(int[] arr, int len) {
System.out.println(Arrays.toString(arr));
}
最后,您的main
方法应调用方法method1
和method2
来填充数组并显示它。像
public static void main(String[] args) {
int[] arr = new int[20];
for (int i = 0; i < arr.length; i++) {
arr[i] = method1(1, 10);
}
method2(arr, arr.length);
}
答案 1 :(得分:1)
您已声明未使用的变量并传递不必要的变量。
可以通过在main中声明Array
然后在两个函数中仅传递数组来轻松完成。您无需传递SIZE
和其他变量。
以下是代码段:
class Main {
private static final Random RND_GEN = new Random();
private void createNum(int[] randomNumbers) {
for (int i = 0; i < randomNumbers.length; i++) {
randomNumbers[i] = RND_GEN.nextInt(10) + 1;
}
}
private void printNum(int[] randomNumbers){
for (int i = 0; i < randomNumbers.length; i++) {
System.out.println("Number " + i + " : " + randomNumbers[i]);
}
}
private void run() {
Scanner inputReader = new Scanner(System.in);
int x = 1;
do {
int[] number = new int[20];
createNum(number);
printNum(number);
System.out.print("Restart Program?, Enter 1 for YES, 2 for NO: ");
x = inputReader.nextInt();
} while (x == 1);
}
public static void main(String[] args) {
Main me = new Main();
me.run();
}
}
输入:
2
输出:
Number 0 : 4
Number 1 : 4
Number 2 : 2
Number 3 : 2
Number 4 : 6
Number 5 : 1
Number 6 : 2
Number 7 : 7
Number 8 : 9
Number 9 : 8
Number 10 : 5
Number 11 : 2
Number 12 : 9
Number 13 : 1
Number 14 : 10
Number 15 : 6
Number 16 : 8
Number 17 : 1
Number 18 : 2
Number 19 : 6