Java输出中的2维数组

时间:2015-09-17 11:28:41

标签: java

我很难输出我尝试运行的阵列中每行的平均值:

    int x, y, val;
    double sum=0, ave, sumtot;
    System.out.print("Enter rows: ");
    x = khel.nextInt();
    System.out.print("Enter columns: ");
    y = khel.nextInt();
    double[][] myArray = new double[x][y];
    val = x * y;
    System.out.print("Enter " + val + " numbers: ");
    for(int i=0; i<myArray.length; i++)
    {
        for(int j=0; j<myArray[i].length; j++)
        {
            myArray[i][j] = khel.nextDouble();
        }
    }
    System.out.println("My Array Table:");
    for(int i=0; i<myArray.length; i++)
    {
        for(int j=0; j<myArray[i].length; j++)
        {
            System.out.print("" + myArray[i][j] + "\t");
            sum = sum + myArray[i][j];
        }
        sumtot = sum;
        System.out.println("");
    }
    System.out.println("The sum of all numbers is " + sum + ".");
    ave = sum / val;
    System.out.println("The average of all numbers is " + ave + ".");

希望你们可以帮助我。:)

4 个答案:

答案 0 :(得分:2)

您可能想尝试这样的事情(略微清理代码)。如果您需要存储每行平均值的数据而不是仅仅在飞行中打印它们,您可以将它们添加到数组或arraylist中。

double sum=0, ave;

int x = khel.nextInt();
int y = khel.nextInt(); // input for array sizes
int val = x * y;

double rowSum; // something to contain data

double[][] myArray = new double[x][y];

for(int i=0; i < myArray.length; i++)
{
    for(int j=0; j < myArray[i].length; j++)
    {
        myArray[i][j] = khel.nextDouble(); // input for array content
    }
}

for(int i=0; i < myArray.length; i++)
{
    rowSum = 0; // reset value each row

    for(int j=0; j < myArray[i].length; j++)
    {

        System.out.print(myArray[i][j] + "\t");
        sum +=  myArray[i][j];

        rowSum += myArray[i][j]; // add value from array
    }
    System.out.println(rowSum / (double)myArray[i].length); // print average from each row

    System.out.println("");
}
System.out.println("The sum of all numbers is " + sum + ".");

// this is the average of the whole table
ave = sum / val; 
System.out.println("The average of all numbers is " + ave + ".");

答案 1 :(得分:0)

查看此逻辑并将其应用于您的代码中(假设数组[5] [5]已有值)...

for( int row = 0; row < 5; row++ )
{
    int rowSum = 0;

    for( int column = 0; column < 5; column++ )
    {
        rowSum += array[row][column];
    }

    System.out.println( String.format( "Sum of values in row %d: %d", row, rowSum ) );
    System.out.println( String.format( "Average of values in row %d: %d\n", row, ( rowSum / 5 ) ) );
}

答案 2 :(得分:0)

如果您只想输出每行的平均值,请根据您当前的行进行编辑。你可以这样做:

for(int i=0; i<myArray.length; i++)
{
    for(int j=0; j<myArray[i].length; j++)
    {
        rowSum += myArray[i][j];    //Calculate sum of each row
    }
    System.out.println("Avg for row " + (i+1) + ":" + rowSum/(double)myArray.length);
    rowSum = 0;     //reset value for calculation for next row 
}

记下我放置println()语句的位置。将它放在内部循环之后将确保在每行中添加所有列值后进行打印。

答案 3 :(得分:0)

我终于明白了!谢谢你们所有的输出人员!:)它越来越接近正确了。:)以下是我的表现:

    int x, y, val, i, j, row=1;
    double sum=0, ave, rowSum;
    System.out.print("Enter rows: ");
    x = khel.nextInt();
    System.out.print("Enter columns: ");
    y = khel.nextInt();
    double[][] myArray = new double[x][y];
    val = x * y;
    System.out.print("Enter " + val + " numbers: ");
    for(i=0; i<myArray.length; i++)
    {
        for(j=0; j<myArray[i].length; j++)
        {
            myArray[i][j] = khel.nextDouble();
        }
    }
    System.out.println("My Array Table:");
    for(i=0; i<myArray.length; i++)
    {
        rowSum = 0;
        for(j=0; j<myArray[i].length; j++)
        {
            System.out.print("" + myArray[i][j] + "\t");
            sum = sum + myArray[i][j];
            rowSum = rowSum + myArray[i][j];
        }
        rowSum = rowSum / myArray[i].length;
        System.out.println("");
        System.out.println("The average of the row " + row + " is " + rowSum + ".");
        row++;
    }
    System.out.println("The sum of all numbers is " + sum + ".");
    ave = sum / val;
    System.out.println("The average of all numbers is " + ave + ".");
}   

}

你的投入给了我很多帮助。非常感谢。:)