Two method code that prints an array of 1-20 with a span of 10
主要方法如下:
这是我目前为止的代码,有几个错误:
1 2 3 4 5 6 | 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 |
在这品脱的时候,我感到有点迷茫,提前谢谢:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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); } } |
号
你的说明对我来说已经足够清楚了,让我们从
1 2 3 4 |
接下来,您需要传递一个数组,它的长度为
号
最后,您的
1 2 3 4 5 6 7 | 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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(); } } |
。
输入:
1 | 2 |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
。