需要帮助 nest for loop 将 table 中的所有行和列添加到 java
Need assistance nest for loop to add all rrows and colums in table in java
题目是:读取table中给定的一组数字,并显示总数。 (添加行和列)
这是我到目前为止所拥有的。我需要有关如何获得正确输出的指导。提前致谢。
*不想用数组
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);
}
}
下面的程序会要求用户先分别插入行数和列数。接下来,使用嵌套的 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);
}
}
进口java.util.Scanner;
public class 整数表 {
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);
}
}
题目是:读取table中给定的一组数字,并显示总数。 (添加行和列) 这是我到目前为止所拥有的。我需要有关如何获得正确输出的指导。提前致谢。
*不想用数组
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);
}
}
下面的程序会要求用户先分别插入行数和列数。接下来,使用嵌套的 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);
}
}
进口java.util.Scanner; public class 整数表 {
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);
}
}