Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查找5x5数组的和_Java_Arrays - Fatal编程技术网

Java 查找5x5数组的和

Java 查找5x5数组的和,java,arrays,Java,Arrays,我的代码有问题。我试图找到这个5x5数组的和,但它总是给我一个0的总数。当我使用2x2阵列时,它可以工作,但对于5x5阵列则无法工作。有人能帮忙吗 import java.util.*; public class QuestionOne { public static void main(String[] args) { Random rand = new Random(); int num1=0, num2=0, num3=0, num4=0, num5=0;

我的代码有问题。我试图找到这个5x5数组的和,但它总是给我一个0的总数。当我使用2x2阵列时,它可以工作,但对于5x5阵列则无法工作。有人能帮忙吗

import java.util.*;

public class QuestionOne
{
  public static void main(String[] args) 
  {
    Random rand = new Random();
    int num1=0, num2=0, num3=0, num4=0, num5=0;
    int [][] numArray = new int [5][5];
    int average =0, totalRow=0;
    int highestVal=0, lowestVal=0;

    for (int row = 0; row < 5; row++)
    {
      num1 = rand.nextInt(1000) + 1;
      num4 =rand.nextInt(1000) + 1;
      for (int col = 0; col < 4; col++)
      {
        num2 = rand.nextInt(1000) + 1;
        num5 = rand.nextInt(1000) + 1;
      }
      num3 = rand.nextInt(1000) + 1;
      System.out.println(num1+" " +num2+" " +num3 +" " +num4 +" " +num5);
    }

    //Sum all values    
    int total;
    total =0;
    for (int row = 0; row < numArray.length; row++)
    {
      for (int col = 0; col < numArray[row].length; col++)
      {
        total = total + numArray[row][col];
      }
    }

    System.out.println("The total is " + total);
//System.out.println(numArray.length);
import java.util.*;
公开课问题一
{
公共静态void main(字符串[]args)
{
Random rand=新的Random();
int num1=0,num2=0,num3=0,num4=0,num5=0;
int[]numArray=新int[5][5];
整数平均值=0,总计行=0;
int highestVal=0,lowerstval=0;
对于(int行=0;行<5;行++)
{
num1=rand.nextInt(1000)+1;
num4=兰特·耐克斯汀(1000)+1;
for(int col=0;col<4;col++)
{
num2=兰特·耐克斯汀(1000)+1;
num5=兰特·耐克斯汀(1000)+1;
}
num3=兰特·耐克斯汀(1000)+1;
System.out.println(num1+“”+num2+“”+num3+“”+num4+“”+num5);
}
//对所有值求和
整数合计;
总数=0;
for(int row=0;row
这里的问题是数组中没有设置值

请查找以下工作代码

public static void main(String[] args) {
        Random rand = new Random();
        int num1=0, num2=0, num3=0, num4=0, num5=0;
        int [][] numArray = new int [5][5];
        int average =0, totalRow=0;
        int highestVal=0, lowestVal=0;

        for (int row = 0; row < 5; row++)
        {
          for (int col = 0; col < 5; col++)
          {
            num5 = rand.nextInt(1000) + 1;
            numArray[row][col] = num5;
          }
        }

        //Sum all values    
        int total;
        total =0;
        System.out.println(numArray.length);
        for (int row = 0; row < numArray.length; row++)
        {
          for (int col = 0; col < numArray[row].length; col++)
          {
            total = total + numArray[row][col];
            System.out.println("Row : " + row + "/Col : " + col);
            System.out.println("Total : " + total + "/value : " + numArray[row][col]);
          }
        }

        System.out.println("The total is " + total);

    }
publicstaticvoidmain(字符串[]args){
Random rand=新的Random();
int num1=0,num2=0,num3=0,num4=0,num5=0;
int[]numArray=新int[5][5];
整数平均值=0,总计行=0;
int highestVal=0,lowerstval=0;
对于(int行=0;行<5;行++)
{
for(int col=0;col<5;col++)
{
num5=兰特·耐克斯汀(1000)+1;
努马拉伊[世界其他地区][col]=num5;
}
}
//对所有值求和
整数合计;
总数=0;
System.out.println(numArray.length);
for(int row=0;row
您的随机数生成器没有在数组中存储值,因此您的程序没有添加任何内容

就我个人而言,这是我更直接的方法:

  • 使用嵌套for循环,并通过
    数组[i][j]=Math.random()函数进行循环
  • 创建一个(sum)变量并初始化为0
  • 使用另一个嵌套for循环并执行:
    sum+=array[i][j]

  • 除非您实际开始计算总数,否则您没有使用
    numArray
    。如果您没有在那里存储任何内容,它不应该默认为0吗?请详细说明一下?您从未将任何值放入数组中。当没有任何内容可求和时,您希望求和的具体内容是什么?应该填充数组的代码从不接触数组。我有一个随机数生成,它输入1-1000@KenWhiteYes的随机值。你确实生成了。但你从不将这些值存储在数组中。你生成它们并扔掉。看看你是如何对数组求和的。看看你如何访问特定元素?在代码中你的随机数在哪里你有没有提到过numarray
    ?让海报自己解决这个问题会更好。他们会学到一些东西,而不是如何复制/粘贴别人的作品。