【问题标题】:2D Array to take float inputs from user and calculate sum of columns二维数组从用户获取浮点输入并计算列的总和
【发布时间】:2024-01-03 22:49:01
【问题描述】:

我真的是 Java 新手,我的任务是创建一个接收用户输入的二维数组。我遇到的问题是它需要接受一些十进制数以及整数,然后计算每列的总和,一旦输入十进制数,我的程序就会返回错误。我尝试将“int”的所有实例更改为“float”,但仍然弹出相同的错误。然后我还需要打印出每列的总数。

到目前为止我的代码是:

    package sumElements;
    import java.util.Scanner;



    public class sumElements{



    public static void main(String args[]){
    int row, col, i, j;
    int arr[][] = new int[3][4];
    Scanner scan = new Scanner(System.in);

    // enter row and column for array.
    row = 3;
    col = 4;

    // enter array elements.
    System.out.println("Enter " +(row*col)+ " array elements row by row (4 per row) : ");
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
        {
            arr[i][j] = scan.nextInt();
        }
    }

    // the 2D array is here.
    System.out.print("The Array is :\n");
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
        {
            System.out.print(arr[i][j]+ "  ");
        }
        System.out.println();
    }



   }

}

更改代码:

package sumElements;
import java.util.Scanner;



public class sumElements{



public static void main(String args[]){
    float row, col, i, j;
    float arr[][] = new float[3][4];
    Scanner scan = new Scanner(System.in);

    // enter row and column for array.
    row = 3;
    col = 4;

    // enter array elements.
    System.out.println("Enter " +(row*col)+ " array elements row by row (4 per row) : ");
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
        {
            arr[i][j] = scan.nextFloat();
        }
    }

    // the 2D array is here.
    System.out.print("The Array is :\n");
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
        {
            System.out.print(arr[i][j]+ "  ");
        }
        System.out.println();
    }



}

}

【问题讨论】:

  • “一些错误”弹出窗口,你能更具体一点吗?另外,阅读命名约定(例如关于类名),它会让你的代码更容易阅读
  • 您遇到什么错误?这段代码显然只接受整数。
  • 对不起,我应该提到这一点。将 int 的所有实例更改为 float 后,我​​在第 25 行收到错误:arr[i] [j] = scan.nextFloat();
  • 错误是什么?
  • 它说它不能将“int”转换为“float”......我已将所有“ints”更改为“float”。有具体的方法吗?

标签: java sum decimal multiple-columns


【解决方案1】:
package sumElements;
import java.util.Scanner;



public class sumElements{



public static void main(String args[]){
    int row, col, i, j;
    float arr[][] = new float[3][4];
    Scanner scan = new Scanner(System.in);

    // enter row and column for array.
    row = 3;
    col = 4;

    // enter array elements.
    System.out.println("Enter " +(row*col)+ " array elements row by row (4 per row) : ");
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
        {
            arr[i][j] = scan.nextFloat();
        }
    }

    // the 2D array is here.
    System.out.print("The Array is :\n");
    for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
        {
            System.out.print(arr[i][j]+ "  ");
        }
        System.out.println();
    }



}

索引必须是 int,因为数组的元素是浮动的

【讨论】:

  • 感谢吉米的建议,但一旦我输入了一个十进制数字。我收到一个错误...线程“main”中的异常
  • 它以浮点数的形式返回数组,但实际上并未将它们放入。示例:5.0 2.0 3.0。 1.0 但只要我输入一个十进制数 (2.4) 它就会崩溃
最近更新 更多