java中逐列求和
{ //每列总和 } 请帮帮我谢谢 我会使用java中逐列求和,java,arrays,io,Java,Arrays,Io,{ //每列总和 } 请帮帮我谢谢 我会使用文件来清理我的扫描仪。此外,您可以围绕输入的行构建一个扫描器,以获取int列(这不需要关闭,因为字符串(s)无论如何都不可关闭)。大概 i have file txt in desktop : 1 5 23 2 5 25 3 30 36 i want sum column by column 1 + 2 + 3 =... and 5 + 5...n and 23,...n Scanner sc = new Sc
文件来清理我的扫描仪
。此外,您可以围绕输入的行
构建一个扫描器
,以获取int
列(这不需要关闭,因为字符串
(s)无论如何都不可关闭)。大概
i have file txt in desktop :
1 5 23
2 5 25
3 30 36
i want sum column by column 1 + 2 + 3 =... and 5 + 5...n and 23,...n
Scanner sc = new Scanner (file("patch");
while (sc.hasNextLine)
作为构造函数Javadoc文档
构造一个新的扫描仪
,该扫描仪生成从指定字符串扫描的值
编辑对列求和有点棘手,但您可以将所有内容读入多维列表
中,如
try (Scanner sc = new Scanner(new File("patch"))) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner row = new Scanner(line);
long sum = 0;
int count = 0;
while (row.hasNextInt()) {
int val = row.nextInt();
if (count == 0) {
System.out.print(val);
} else {
System.out.printf(" + %d", val);
}
sum += val;
count++;
}
System.out.println(" = " + sum);
}
} catch (IOException e) {
e.printStackTrace();
}
try (Scanner sc = new Scanner(new File("patch"))) {
List<List<Integer>> rows = new ArrayList<>();
int colCount = 0;
while (sc.hasNextLine()) {
List<Integer> al = new ArrayList<>();
String line = sc.nextLine();
Scanner row = new Scanner(line);
colCount = 0;
while (row.hasNextInt()) {
colCount++;
int val = row.nextInt();
al.add(val);
}
rows.add(al);
}
for (int i = 0; i < colCount; i++) {
long sum = 0;
for (List<Integer> row : rows) {
sum += row.get(i);
}
if (i != 0) {
System.out.print("\t");
}
System.out.print(sum);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
我会使用文件来清理我的扫描仪
。此外,您可以围绕输入的行
构建一个扫描器
,以获取int
列(这不需要关闭,因为字符串
(s)无论如何都不可关闭)。大概
i have file txt in desktop :
1 5 23
2 5 25
3 30 36
i want sum column by column 1 + 2 + 3 =... and 5 + 5...n and 23,...n
Scanner sc = new Scanner (file("patch");
while (sc.hasNextLine)
作为构造函数Javadoc文档
构造一个新的扫描仪
,该扫描仪生成从指定字符串扫描的值
编辑对列求和有点棘手,但您可以将所有内容读入多维列表
中,如
try (Scanner sc = new Scanner(new File("patch"))) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner row = new Scanner(line);
long sum = 0;
int count = 0;
while (row.hasNextInt()) {
int val = row.nextInt();
if (count == 0) {
System.out.print(val);
} else {
System.out.printf(" + %d", val);
}
sum += val;
count++;
}
System.out.println(" = " + sum);
}
} catch (IOException e) {
e.printStackTrace();
}
try (Scanner sc = new Scanner(new File("patch"))) {
List<List<Integer>> rows = new ArrayList<>();
int colCount = 0;
while (sc.hasNextLine()) {
List<Integer> al = new ArrayList<>();
String line = sc.nextLine();
Scanner row = new Scanner(line);
colCount = 0;
while (row.hasNextInt()) {
colCount++;
int val = row.nextInt();
al.add(val);
}
rows.add(al);
}
for (int i = 0; i < colCount; i++) {
long sum = 0;
for (List<Integer> row : rows) {
sum += row.get(i);
}
if (i != 0) {
System.out.print("\t");
}
System.out.print(sum);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
我会使用文件来清理我的扫描仪
。此外,您可以围绕输入的行
构建一个扫描器
,以获取int
列(这不需要关闭,因为字符串
(s)无论如何都不可关闭)。大概
i have file txt in desktop :
1 5 23
2 5 25
3 30 36
i want sum column by column 1 + 2 + 3 =... and 5 + 5...n and 23,...n
Scanner sc = new Scanner (file("patch");
while (sc.hasNextLine)
作为构造函数Javadoc文档
构造一个新的扫描仪
,该扫描仪生成从指定字符串扫描的值
编辑对列求和有点棘手,但您可以将所有内容读入多维列表
中,如
try (Scanner sc = new Scanner(new File("patch"))) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner row = new Scanner(line);
long sum = 0;
int count = 0;
while (row.hasNextInt()) {
int val = row.nextInt();
if (count == 0) {
System.out.print(val);
} else {
System.out.printf(" + %d", val);
}
sum += val;
count++;
}
System.out.println(" = " + sum);
}
} catch (IOException e) {
e.printStackTrace();
}
try (Scanner sc = new Scanner(new File("patch"))) {
List<List<Integer>> rows = new ArrayList<>();
int colCount = 0;
while (sc.hasNextLine()) {
List<Integer> al = new ArrayList<>();
String line = sc.nextLine();
Scanner row = new Scanner(line);
colCount = 0;
while (row.hasNextInt()) {
colCount++;
int val = row.nextInt();
al.add(val);
}
rows.add(al);
}
for (int i = 0; i < colCount; i++) {
long sum = 0;
for (List<Integer> row : rows) {
sum += row.get(i);
}
if (i != 0) {
System.out.print("\t");
}
System.out.print(sum);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
我会使用文件来清理我的扫描仪
。此外,您可以围绕输入的行
构建一个扫描器
,以获取int
列(这不需要关闭,因为字符串
(s)无论如何都不可关闭)。大概
i have file txt in desktop :
1 5 23
2 5 25
3 30 36
i want sum column by column 1 + 2 + 3 =... and 5 + 5...n and 23,...n
Scanner sc = new Scanner (file("patch");
while (sc.hasNextLine)
作为构造函数Javadoc文档
构造一个新的扫描仪
,该扫描仪生成从指定字符串扫描的值
编辑对列求和有点棘手,但您可以将所有内容读入多维列表
中,如
try (Scanner sc = new Scanner(new File("patch"))) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner row = new Scanner(line);
long sum = 0;
int count = 0;
while (row.hasNextInt()) {
int val = row.nextInt();
if (count == 0) {
System.out.print(val);
} else {
System.out.printf(" + %d", val);
}
sum += val;
count++;
}
System.out.println(" = " + sum);
}
} catch (IOException e) {
e.printStackTrace();
}
try (Scanner sc = new Scanner(new File("patch"))) {
List<List<Integer>> rows = new ArrayList<>();
int colCount = 0;
while (sc.hasNextLine()) {
List<Integer> al = new ArrayList<>();
String line = sc.nextLine();
Scanner row = new Scanner(line);
colCount = 0;
while (row.hasNextInt()) {
colCount++;
int val = row.nextInt();
al.add(val);
}
rows.add(al);
}
for (int i = 0; i < colCount; i++) {
long sum = 0;
for (List<Integer> row : rows) {
sum += row.get(i);
}
if (i != 0) {
System.out.print("\t");
}
System.out.print(sum);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
如果文件是CSV格式的,则按逗号(“,”)拆分行,并根据拆分数组长度查找列数
如下图所示:
try (Scanner sc = new Scanner(new File("patch"))) {
Map<Integer, Integer> cols = new HashMap<>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner row = new Scanner(line);
int colCount = 0;
while (row.hasNextInt()) {
int val = row.nextInt();
if (cols.containsKey(colCount)) {
val += cols.get(colCount);
}
cols.put(colCount, val);
colCount++;
}
}
for (int i : cols.values()) {
System.out.printf("%d\t", i);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
创建大小为len的arraylist数组,并将每个列字段存储在相应的arraylist中。
最后,在每个arraylist的末尾应用sum来计算每个列值的总和。如果文件是CSV格式的,则按逗号(“,”)拆分行,并根据拆分的数组长度查找列数
如下图所示:
try (Scanner sc = new Scanner(new File("patch"))) {
Map<Integer, Integer> cols = new HashMap<>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner row = new Scanner(line);
int colCount = 0;
while (row.hasNextInt()) {
int val = row.nextInt();
if (cols.containsKey(colCount)) {
val += cols.get(colCount);
}
cols.put(colCount, val);
colCount++;
}
}
for (int i : cols.values()) {
System.out.printf("%d\t", i);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
创建大小为len的arraylist数组,并将每个列字段存储在相应的arraylist中。
最后,在每个arraylist的末尾应用sum来计算每个列值的总和。如果文件是CSV格式的,则按逗号(“,”)拆分行,并根据拆分的数组长度查找列数
如下图所示:
try (Scanner sc = new Scanner(new File("patch"))) {
Map<Integer, Integer> cols = new HashMap<>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner row = new Scanner(line);
int colCount = 0;
while (row.hasNextInt()) {
int val = row.nextInt();
if (cols.containsKey(colCount)) {
val += cols.get(colCount);
}
cols.put(colCount, val);
colCount++;
}
}
for (int i : cols.values()) {
System.out.printf("%d\t", i);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
创建大小为len的arraylist数组,并将每个列字段存储在相应的arraylist中。
最后,在每个arraylist的末尾应用sum来计算每个列值的总和。如果文件是CSV格式的,则按逗号(“,”)拆分行,并根据拆分的数组长度查找列数
如下图所示:
try (Scanner sc = new Scanner(new File("patch"))) {
Map<Integer, Integer> cols = new HashMap<>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner row = new Scanner(line);
int colCount = 0;
while (row.hasNextInt()) {
int val = row.nextInt();
if (cols.containsKey(colCount)) {
val += cols.get(colCount);
}
cols.put(colCount, val);
colCount++;
}
}
for (int i : cols.values()) {
System.out.printf("%d\t", i);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
创建大小为len的arraylist数组,并将每个列字段存储在相应的arraylist中。
最后,在最后对每个arraylist应用sum来计算每个列值的总和。请查找代码。请仔细阅读评论。
这是为您的参考做的一种方法。我希望您尝试其他方法来提高您的知识,而不仅仅是使用此代码
String line = sc.next();
String[] lineArr = line.split(",");
int len = lineArr.length;
请找到密码。请仔细阅读评论。
这是为您的参考做的一种方法。我希望您尝试其他方法来提高您的知识,而不仅仅是使用此代码
String line = sc.next();
String[] lineArr = line.split(",");
int len = lineArr.length;
请找到密码。请仔细阅读评论。
这是为您的参考做的一种方法。我希望您尝试其他方法来提高您的知识,而不仅仅是使用此代码
String line = sc.next();
String[] lineArr = line.split(",");
int len = lineArr.length;
请找到密码。请仔细阅读评论。
这是为您的参考做的一种方法。我希望您尝试其他方法来提高您的知识,而不仅仅是使用此代码
String line = sc.next();
String[] lineArr = line.split(",");
int len = lineArr.length;
如果您知道每行有多少个条目,您可以为总和创建一个int[]
。然后,在读取每一行之后,拆分、解析并增加每一行的总和。@DavidWallace您可以使用2d数组,而不知道每行有多少个条目,但它会变得更复杂。@MarshallTigerus您可以,但您必须有点疯狂。如果您不知道每行有多少个条目,那么应该使用ArrayList
,并在何时向其中添加新条目时使用一些奇特的逻辑。有点疯狂或有点字节疯狂。除了数组之外,还有很多更好的方法。如果你知道每行有多少个条目,你可以为求和做一个int[]
。然后,在读取每一行之后,拆分、解析并增加每一行的总和。@DavidWallace您可以使用2d数组,而不知道每行有多少个条目,但它会变得更复杂。@MarshallTigerus您可以,但您必须有点疯狂。如果您不知道每行有多少个条目,那么应该使用ArrayList
,并在何时向其中添加新条目时使用一些奇特的逻辑。有点疯狂或有点字节疯狂。有很多更好的方法