查找列中所有值的总和 [RNG]
Finding Total of all Values in a Column [RNG]
我正在开发一个程序,打印出 table 填充随机生成的整数。大多数逻辑都相对简单,并且运行良好。我想做的一件事是累加并打印出每一列的总值,以及 table 中所有值的最终总和。我四处寻找答案,但一直没有找到答案。
rowNbr = 7; // ROW CONTROLS
colNbr = 5; //COLUMN CONTROLS
rpt01 = String.format("%0" + (colNbr-1) + "d", 0).replace("0",dash); //Redefine rpt01
colBld = String.format("|---------------|%s\n",rpt01); //Redefine colBld
String cnrTxt = "First Quarter";
System.out.printf(colBld);
System.out.printf("|%-15s",cnrTxt);
for(int i = 1; i < colNbr; i = i++){ //Open for loop (columns)
String regTxt = "Region " + i++;
System.out.printf("|%-10s",regTxt);
} //End for
System.out.printf("|\n");
//Initialize array
int sales[] = new int[100];
int idx = 0;
for(int i = 1; i <= rowNbr-3; i++){
String prodTxt = "Product " + i;
System.out.printf(colBld);
System.out.printf("|%-15s|",prodTxt);
for(int j = 0; j < colNbr-1; j++){ //Open for loop (columns 2)
sales[idx] = (int)(Math.random() * 16 + 1);
System.out.printf("%-10d|",sales[idx]);
idx++;
} //End for
System.out.printf("\n");
} //End for
int totalNbr = 0; //Placeholder zero
int regNbr = 0; //Placeholder zero
String totalTxt = "Final Total: ";
String regTxt = "Region Totals";
System.out.printf(colBld);
System.out.printf("|%-15s|%-10s|\n",regTxt,regNbr);
System.out.printf(colBld);
System.out.printf("|%s%s\n",totalTxt,totalNbr);
System.out.printf(colBld);
这是代码目前的样子 运行:
|---------------|----------|----------|----------|----------|
|First Quarter |Region 1 |Region 2 |Region 3 |Region 4 |
|---------------|----------|----------|----------|----------|
|Product 1 |2 |10 |3 |1 |
|---------------|----------|----------|----------|----------|
|Product 2 |15 |15 |7 |16 |
|---------------|----------|----------|----------|----------|
|Product 3 |15 |13 |7 |9 |
|---------------|----------|----------|----------|----------|
|Product 4 |4 |14 |11 |11 |
|---------------|----------|----------|----------|----------|
|Region Totals |0 |
|---------------|----------|----------|----------|----------|
|Final total: 0
|---------------|----------|----------|----------|----------|
老实说,我什至不知道从哪里开始。感谢您的帮助!
由于您已经使用随机整数值顺序填充 sales 数组以表示顺序显示的区域,因此只需按数字递增逐步遍历数组元素即可区域并对每个步骤中检测到的元素求和。
要确定将要显示的 Regions 的实际数量以及要跟踪的 Region Totals 的数量,我们可以根据它来自 colNbr 变量中包含的值减去 1(将显示 5 - 1 = 4 个区域)。
知道这一点后,我们声明了另一个名为 regionTotals 的整数数组,并将其长度设为 4:
int[] regionTotals = new int[colNbr-1];
现在是单步执行 sales 数组元素并将正确的 sales 数组元素相加到正确的 regionTotal 元素,像这样:
int regionCols = colNbr-1;
int[] regionTotals = new int[regionCols];
int stepCounter = 0;
for (int i = 0; i < sales.length; i++) {
stepCounter++;
if (stepCounter > regionCols) { stepCounter = 1; }
int regionIndex = (stepCounter - 1);
regionTotals[regionIndex]+= sales[i];
}
System.out.println("\nRegion Totals: " + Arrays.toString(regionTotals) + "\n");
将上面的代码直接放在totalNbr整数变量的声明和初始化上面:
int totalNbr = 0; //Placeholder zero
如何将 regionTotals 数组元素格式化为 table 取决于您,但请记住,数组是从零开始的,因此索引 0 处的元素数组包含区域 1 的总和。
我正在开发一个程序,打印出 table 填充随机生成的整数。大多数逻辑都相对简单,并且运行良好。我想做的一件事是累加并打印出每一列的总值,以及 table 中所有值的最终总和。我四处寻找答案,但一直没有找到答案。
rowNbr = 7; // ROW CONTROLS
colNbr = 5; //COLUMN CONTROLS
rpt01 = String.format("%0" + (colNbr-1) + "d", 0).replace("0",dash); //Redefine rpt01
colBld = String.format("|---------------|%s\n",rpt01); //Redefine colBld
String cnrTxt = "First Quarter";
System.out.printf(colBld);
System.out.printf("|%-15s",cnrTxt);
for(int i = 1; i < colNbr; i = i++){ //Open for loop (columns)
String regTxt = "Region " + i++;
System.out.printf("|%-10s",regTxt);
} //End for
System.out.printf("|\n");
//Initialize array
int sales[] = new int[100];
int idx = 0;
for(int i = 1; i <= rowNbr-3; i++){
String prodTxt = "Product " + i;
System.out.printf(colBld);
System.out.printf("|%-15s|",prodTxt);
for(int j = 0; j < colNbr-1; j++){ //Open for loop (columns 2)
sales[idx] = (int)(Math.random() * 16 + 1);
System.out.printf("%-10d|",sales[idx]);
idx++;
} //End for
System.out.printf("\n");
} //End for
int totalNbr = 0; //Placeholder zero
int regNbr = 0; //Placeholder zero
String totalTxt = "Final Total: ";
String regTxt = "Region Totals";
System.out.printf(colBld);
System.out.printf("|%-15s|%-10s|\n",regTxt,regNbr);
System.out.printf(colBld);
System.out.printf("|%s%s\n",totalTxt,totalNbr);
System.out.printf(colBld);
这是代码目前的样子 运行:
|---------------|----------|----------|----------|----------|
|First Quarter |Region 1 |Region 2 |Region 3 |Region 4 |
|---------------|----------|----------|----------|----------|
|Product 1 |2 |10 |3 |1 |
|---------------|----------|----------|----------|----------|
|Product 2 |15 |15 |7 |16 |
|---------------|----------|----------|----------|----------|
|Product 3 |15 |13 |7 |9 |
|---------------|----------|----------|----------|----------|
|Product 4 |4 |14 |11 |11 |
|---------------|----------|----------|----------|----------|
|Region Totals |0 |
|---------------|----------|----------|----------|----------|
|Final total: 0
|---------------|----------|----------|----------|----------|
老实说,我什至不知道从哪里开始。感谢您的帮助!
由于您已经使用随机整数值顺序填充 sales 数组以表示顺序显示的区域,因此只需按数字递增逐步遍历数组元素即可区域并对每个步骤中检测到的元素求和。
要确定将要显示的 Regions 的实际数量以及要跟踪的 Region Totals 的数量,我们可以根据它来自 colNbr 变量中包含的值减去 1(将显示 5 - 1 = 4 个区域)。
知道这一点后,我们声明了另一个名为 regionTotals 的整数数组,并将其长度设为 4:
int[] regionTotals = new int[colNbr-1];
现在是单步执行 sales 数组元素并将正确的 sales 数组元素相加到正确的 regionTotal 元素,像这样:
int regionCols = colNbr-1;
int[] regionTotals = new int[regionCols];
int stepCounter = 0;
for (int i = 0; i < sales.length; i++) {
stepCounter++;
if (stepCounter > regionCols) { stepCounter = 1; }
int regionIndex = (stepCounter - 1);
regionTotals[regionIndex]+= sales[i];
}
System.out.println("\nRegion Totals: " + Arrays.toString(regionTotals) + "\n");
将上面的代码直接放在totalNbr整数变量的声明和初始化上面:
int totalNbr = 0; //Placeholder zero
如何将 regionTotals 数组元素格式化为 table 取决于您,但请记住,数组是从零开始的,因此索引 0 处的元素数组包含区域 1 的总和。