英文:
How to keep track of the sum of each of the three columns Java
问题
以下是翻译的内容:
我的问题是如何让我的程序分别对每一列进行加法,然后再求平均值。例如,在Java中表示为 33+42+11 / 3。
希望这样能澄清问题。
我还附上了下面的数字和列:
import java.util.Arrays;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
public class Assignment {
public static void main(String[] args) throws IOException {
FileInputStream control = null;
Scanner scanner = null;
if (args.length == 0) {
System.out.println("需要文件名");
System.exit(0);
}
System.out.println("正在打开文件 " + args[0]);
control = new FileInputStream(args[0]);
System.out.println("成功打开文件 " + args[0]);
scanner = new Scanner(control);
long sum = 0;
int count = 0;
if (!scanner.hasNext()) {
System.out.println("没有整数");
System.exit(0);
}
while (scanner.hasNext()) {
long number = scanner.nextInt();
sum += number;
count++;
}
long average = sum / count;
System.out.println(average);
control.close();
}
}
这是列。不太确定如何让它从 127 开始依次向下加到 10,其他两列也是一样:
127 33 22
2147483647 42 59
10 11 55
英文:
My Question is how to get my program to add each column separately and then find the average afterwards. E.g. 33+42+11 /3 expressed in Java.
Hope that clarifies the question.
I have also linked the numbers and columns below
import java.util.Arrays;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
public class Assignment
{
public static void main(String[] args)
throws IOException
{
FileInputStream control = null;
Scanner scanner = null;
if (args.length == 0) {
System.out.println("file name expected");
System.exit(0);
}
System.out.println("Opening file " + args[0]);
control = new FileInputStream(args[0]);
System.out.println("Opening of file " + args[0] + " successful");
scanner = new Scanner(control);
long sum = 0;
int count = 0;
if (!scanner.hasNext()) {
System.out.println("No integers");
System.exit(0);
}
while (scanner.hasNext()) {
long number = scanner.nextInt();
sum += number;
count++;
}
long average = sum / count;
System.out.println(average);
control.close();
}
}
This is the columns. Not too sure about how to make it start adding from 127 down to 10 and same for the other two columns
127 33 22
2147483647 42 59
10 11 55
答案1
得分: 1
我认为我能够解释这个问题。
以下是一个可行的解决方案 - 请注意,您需要使用一个扫描器逐行扫描,然后扫描每一行将其拆分为列。然后,我将这些和与计数存储在一个数组中,正如您提到您想要使用数组一样。但是,我只是让这些数组最多存储10个计数,并且没有在这些数组上进行任何大小检查 - 这些类型的问题似乎超出了原始问题的范围。
import java.util.Scanner;
public class Main {
public static void main(String[] args){
String fakeFileInput = " 127 33 22\n2147483647 42 59\n10 11 55";
Scanner scanner = new Scanner(fakeFileInput);
if (!scanner.hasNext()) {
System.out.println("No integers");
System.exit(0);
}
long[] sums = new long[10];
int[] counts = new int[10];
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Scanner innerScan = new Scanner(line);
int current = 0;
while (innerScan.hasNextInt()) {
int number = innerScan.nextInt();
sums[current] += number;
counts[current] += 1;
current++;
}
}
for (int i = 0; i < sums.length; i++) {
System.out.println((1.0*sums[i])/counts[i] + " average for " + i + " column");
}
}
}
英文:
I think I was able to interpret the question.
Here is a working solution - note that you need to scan for one line at a time with one scanner, and then scan each of those lines to break it up into columns. I then stored each of those sums and counts in an array, as you mentioned you wanted to use arrays. However, I just made those arrays hold up to 10 counts, and didn't do any sizing checks on those - those types of issues seemed outside the scope of the original question.
https://repl.it/repls/TintedNarrowRectangle
import java.util.Scanner;
public class Main {
public static void main(String[] args){
String fakeFileInput = " 127 33 22\n2147483647 42 59\n10 11 55";
Scanner scanner = new Scanner(fakeFileInput);
if (!scanner.hasNext()) {
System.out.println("No integers");
System.exit(0);
}
long[] sums = new long[10];
int[] counts = new int[10];
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Scanner innerScan = new Scanner(line);
int current = 0;
while (innerScan.hasNextInt()) {
int number = innerScan.nextInt();
sums[current] += number;
counts[current] += 1;
current++;
}
}
for (int i = 0; i < sums.length; i++) {
System.out.println((1.0*sums[i])/counts[i] + " average for " + i + " column");
}
}
}
答案2
得分: 1
我只提供一个3 * 3的网格解决方案,我的目标是保持每列求和。我的最终输出将会是这样的:{(127+2147483647+10),(33+42+11),(22+59+55)}
为此,您只需要维护另一个数组。例如,您将在sum[]数组中保持每列的求和。
由于您只有3列,所以只需要sum[]大小为3。
int[] sum = new int[3];
分配后,您可以将sum数组的每个索引值重置为0。
for (int i = 0; i < 3; i++) {
sum[i] = 0;
}
现在唯一剩下的就是获取每行的输入值。由于每行只有3列,总共有3行。您可以按照以下代码片段操作。
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int number = scanner.nextInt();
sum[i] = sum[i] + number;
}
}
现在您可以使用sum[]数组获得最终的结果。每个sum[]索引保存着每列的求和。
英文:
I am just providing a 3 * 3 grid solution and my goal is to keep each column summation. My final output would be like {(127+2147483647+ 10), (33+42+11), (22+59+55)}
For that, you just need to maintain another array. For example, you will keep each column summation in sum[] array.
Since you have only 3 columns, you just need sum[] size 3.
int[] sum = new int[3]
After allocation, you can reset sum arrays each index value 0.
for (int i = 0; i < 3; i++) {
sum[i]=0
}
Now the only thing left is taking each row input value. Since you have only 3 columns in each row and totally of 3 raws. You can follow below code snippet.
for (int i=0;i<3;i++){
for(int j=0;j<3;j++){
int number = scanner.nextInt();
sum[i] = sum[i] + number;
}
}
Now you can use sum[] array for your final result. Each sum[] index holds on each column summation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论