简体   繁体   English

创建二维数组并找到最大值

[英]Creating a 2D array and finding max

I have to write a program that Creates a 2d array and asks the user for row and column size. 我必须编写一个创建2d数组并询问用户行和列大小的程序。

Create 2-D array say m : 创建二维数组说m:

ask the user to input row and column sizes from keyboard (use Scanner) 要求用户从键盘输入行和列的大小(使用扫描仪)

Assuming that the last digit of your ssn number is N, if the user input column size is bigger than N+5 ask the user to reinput the column size 假设您的ssn号的最后一位为N,如果用户输入的列大小大于N + 5,请要求用户重新输入列大小

fill all arrays elements as double numbers in the range of (4.0 , 11.0) by using of random object 使用随机对象将所有数组元素填充为(4.0,11.0)范围内的双数

pass the above array m and call the following two methods 通过上面的数组m并调用以下两个方法

findMaxCol(m) findMaxCol(米)

returnAvg(m) returnAvg(m)

print out the avg of array m 打印出数组m的平均值

In findMaxCol(double[][]array), find and print the largest sum of columns in the 2 d array 在findMaxCol(double [] [] array)中,找到并打印二维数组中最大的列总和

in returnAvg(double[][] array find the average of all elements in the array and return the average 在returnAvg(double [] []数组中找到数组中所有元素的平均值,然后返回平均值

I am having a hard time getting the random class to post double and finding the max column with average. 我很难让随机类发布两倍并找到具有平均值的最大列。

Scanner console=new Scanner(System.in);
    findMaxcoumn();
    returnAvg();

    double Random =new Random().nextDouble();
    int lastdigit=8;
    System.out.println("Write a row");
    int n=console.nextInt();
    System.out.println("write column");
    int y=console.nextInt();

      int [][] array=new int[n][y];
      if(y>lastdigit+5){
        System.out.println("Write a column again");
        int k=console.nextInt();
        int [][] array2=new int[n][k];
        int [][] array2=rand.nextdouble(8.0)+4.0;
        }
        }
        }

       }

It looks like you might need to do some more reading on how arrays work. 看来您可能需要更多地了解数组的工作原理。 You create the array with the line: 使用以下行创建数组:

int [][] array2=new int[n][k];

This should work just fine, but your next line is: 这应该可以正常工作,但是您的下一行是:

int [][] array2=rand.nextdouble(8.0)+4.0;

This one doesn't do anything that I know of. 这个我没有做任何事。 You need to use two loops, one for the first dimension of the array. 您需要使用两个循环,一个用于数组的第一维。 The other is an inner loop for the second dimension. 另一个是第二维的内部循环。 For instance: 例如:

for(int i = 0; i < n; i++) {
  for(int j = 0; j < k; k++) {
    array2[i][k] = rand.nextDouble(8.0) + 4.0;
  }
}

Try reading the documentation from Oracle . 尝试从Oracle阅读文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM