格式问题与列添加数字列[考试] [英] Formatting Issue with Adding Numbers Column by Column [Java]

查看:136
本文介绍了格式问题与列添加数字列[考试]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想说,一切都准确地计算出开始。唯一的问题我已经尝试打印后,我进入了二维数组以及如何格式化我得到每个栏的总数。

I just want to start off by saying that everything calculates accurately. The only problems I have are trying to print back the 2D array I entered and how to format the totals I get for each column.

这是一个3合1的计划的一部分。对于第二部分,我必须在12个号码中一个3x4的二维数组输入。控制台然后返回我进入阵列,并且通过柱之列。

This is part of a 3 in 1 program. For the second part, I must enter in 12 numbers in a 3x4 2D array. The console then returns the array I entered, and the sum column by column.

这是它的外观:

Enter 3 rows and 4 columns:
1 2 3 4
5 6 7 8
9 10 11.2 12.5
You entered:
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0 11.2 12.5
The sums are:
15.0 18.0 21.2 24.5

这是我的code迄今:

This is my code so far:

else if(choice == 2) {
            // declare the 3x4 array
            System.out.print("Enter a 3 by 4 matrix row by row: ");
            double[][] myArray = new double[3][4];
            // set up the array as an input
            for (int i = 0; i < 3; i++)
              for (int j = 0; j < 4; j++)
                myArray[i][j] = input.nextDouble();

            feature2(myArray);

        } // end of choice 2 block

private static void feature2(double[][] myArray){


    System.out.println("You entered: ");
    // return the entered array in double form
    for (int i = 0; i < myArray.length; i++) {
          System.out.print(myArray[i] + " ");
        }
    // calculate the sums column by column and display the results
    for(int column = 0; column < myArray[0].length; column++) {
        double total = 0;
        for(int row = 0; row < myArray.length; row++)
            total += myArray[row][column];
        System.out.println("The sums are: " + total);

    }
} // end of feature 2

由于code表示,这是在控制台上什么内容如下:

As the code stands, this is what reads on the console:

Enter a 3 by 4 matrix row by row: 
1 2 3 4
5 6 7 8
9 10 11.2 12.5
You entered: 
[D@3d4eac69 [D@42a57993 [D@75b84c92 The sums are: 15.0
The sums are: 18.0
The sums are: 21.2
The sums are: 24.5

正如你所看到的,它计算的正确,但它并没有正确地格式化。如果我能有格式化的帮助下,我可以把它从这里开始。

As you can see, it calculates correctly, but it doesn't format correctly. If I could just have help with the formatting, I can take it from here.

推荐答案

要通过二维数组遍历,你需要一个嵌套的循环。 myArray的[I]的值仅仅是索引i的阵列中的一个的地址。要访问二维数组中的数组的元素,你需要访问不仅数组的索引,但也元素的数组中的索引,myArray的[行] [列]。外环应该通过每一行(阵列),和内环应阵列内经过每个柱(数组元素)。最后,打印出的阵列中的一个的元件后,一个新行应被打印的下一个数组元素。下面是它应该是什么样子,

To traverse through a two-dimensional array, you need a nested for-loop. The value of myArray[i] is just the address of one of the arrays at index i. To access the elements of the arrays within the 2D array, you need to access not only the index of the array but also the index of the element within the array, myArray[row][column]. The outer loop should go through each row (array), and the inner loop should go through each column (array element) within the array. Finally, after printing out the elements of one of the arrays, a newline should be printed for the next array's elements. Here's what it should look like,

System.out.println("You entered: ");
    // return the entered array in double form
for (int i = 0; i < myArray.length; i++) {
    for (int j = 0; i < myArray[i].length; j++){
       System.out.print(myArray[i][j] + " ");
    }
    System.out.println();
}
System.out.println("The sums are: ");
// calculate the sums column by column and display the results
    for(int column = 0; column < myArray[0].length; column++) {
        double total = 0;
        for(int row = 0; row < myArray.length; row++)
            total += myArray[row][column];
        System.out.print(total + " ");

    }

这篇关于格式问题与列添加数字列[考试]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆