【问题标题】:Need assistance nest for loop to add all rrows and colums in table in java需要帮助嵌套for循环在java中添加表中的所有行和列
【发布时间】:2019-11-09 15:13:08
【问题描述】:

问题是:读取表格中给出的一组数字,并显示总数。 (添加行和列) 这是我到目前为止所拥有的。我需要有关如何获得正确输出的指导。提前致谢。

*不想使用数组。

import java.util.Scanner;
public class tableintegers {

    public static void main(String[] args) {

        Scanner input=new Scanner(System.in);

        System.out.print("Give the number of rows and number of columns: ");
        int rows=input.nextInt();
        int cols=input.nextInt();

        int total=0, sum=0, numbers=0;
        for(int i=0;i<rows; i++) {

            if (rows>i) { 
                System.out.print("Enter row " +(i+1)+ ":");
                numbers=input.nextInt();
                input.nextInt();
            }

            sum+=numbers;
            total=sum+numbers;
        }
        System.out.println("The grand total is: " +total);

    }

}

【问题讨论】:

  • 你最后想要什么?分享预期的输出?对值求和,打印值?说清楚;)
  • 关于:“读取表格中给出的一组数字”——这听起来就像您可能想从文本文件中读取一样。请说清楚。另外,您当前的代码有什么问题?
  • 感谢您的回复。我想要给定表中所有行的总和。例如,用户希望表是 2 行乘 3 列。输出将添加每一行的所有数字并给出总计。
  • @HovercraftFullOfEels 我希望用户输入行和列以及每行的数字。

标签: java for-loop methods static nested-loops


【解决方案1】:

下面的程序会要求用户先分别插入行数和列数。接下来,使用嵌套的 for 循环要求用户在每一行中插入每一列的值。

第一个 For 循环遍历行数。对于每次迭代,第二个 For 循环将遍历输入的列数。因此要求用户为每一列输入一个值。

为每一行输入的总值将由 sumOfRow 计算。最后,这些值将相加,并显示最终总数。

希望这会有所帮助:)

import java.util.Scanner;
public class tableintegers {

    public static void main(String[] args) {

        Scanner input=new Scanner(System.in);

        System.out.print("Enter the number of rows:");
        int rows=input.nextInt();
        System.out.print("Enter the number of columns:");
        int cols=input.nextInt();

        int total=0, sumOfRow=0, numbers=0;
        for(int i=0; i<rows; i++) {
            sumOfRow = 0;
            System.out.println("\nRow No. " + (i + 1) );
            for(int j=0; j<cols; j++) {
                System.out.print("Enter value for Column " + (j+1) + ": ");
                numbers = input.nextInt();
                sumOfRow += numbers;
            }
            System.out.println("Sum of Row No. " + (i+1) + " Values: " + sumOfRow);
            total += sumOfRow;
        }
        System.out.println("\nThe grand total is: " +total);

    }

}

【讨论】:

  • 非常感谢。我想到了。我有一些和你很相似的东西。我没有将列合并到我的 for 循环中。
【解决方案2】:

导入 java.util.Scanner; 公共类表格整数 {

public static void main(String[] args) {

    Scanner input=new Scanner(System.in);

    System.out.print("Give the number of rows and number of columns: ");
    int rows=input.nextInt();
    int cols=input.nextInt();

    int sumRow=0;
    for(int i=1;i<=rows; i++) { 
        System.out.print("Enter row " +i+ ":"); 
            for(int j=1;j<=cols; j++) { 
                int numbers=input.nextInt();
                sumRow+=numbers;
    }
    }
        System.out.println("The grand total is: " +sumRow);
    }
}   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2013-10-17
    • 1970-01-01
    相关资源
    最近更新 更多