gpt4 book ai didi

java - 将 parseInt 更改为 nextInt

转载 作者:行者123 更新时间:2023-11-30 05:25:16 25 4
gpt4 key购买 nike

我有一些代码可以正常工作并且可以完成它应该做的事情。不幸的是,我只需要使用 nextInt(); 。有人知道该怎么做吗?

当前输入(来自文本文件):

3

4

2 3 6 7

4 5 6 7

2 2 2 2

当前输出:

Average score of each assignment:

Assignment #1 Average: 2.666666666

Assignment #2 Average: 3.333333333

Assignment #3 Average: 4.666666666

Assignment #4 Average: 5.333333333
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class Scores {
public static void main(String[] args) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the name of the file containing the scores?");
String fileName = keyboard.nextLine();
Scanner fileScan = new Scanner(new File(fileName));

//TODO: read in the values for the number of students and number of assignments using the Scanner on the file
//TODO: create a 2-D to store all the scores and read them all in using the Scanner on the file
int rows = Integer.parseInt(fileScan.nextLine());
int columns = Integer.parseInt(fileScan.nextLine());

int [][] myArray = new int[rows][columns];
while(fileScan.hasNextLine()) {
for (int i=0; i<myArray.length; i++) {
String[] line = fileScan.nextLine().trim().split(" ");
for (int j=0; j<line.length; j++) {
myArray[i][j] = Integer.parseInt(line[j]);
}
}
}
System.out.println("Array of scores:");
//TODO: print the entire array, row by row, using Arrays.toString()
System.out.println(Arrays.deepToString(myArray).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));

System.out.println("Average score of each assignment:");
//TODO: compute and print the average on each assignment
double total=0;
int assignment=1;
for(int i=0;i<myArray[0].length;i++) {
for(int j=0;j<myArray.length;j++) {
total+=myArray[j][i];
}
System.out.println("Assignment #" + assignment++ + " Average = " + (total/3));
total=0;
}
fileScan.close();
keyboard.close();
}
}

最佳答案

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Scores {

public static void main(String[] args) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the name of the file containing the scores?");
String fileName = keyboard.nextLine();
Scanner fileScan = new Scanner(new File(fileName));

//TODO: read in the values for the number of students and number of assignments using the Scanner on the file
//TODO: create a 2-D to store all the scores and read them all in using the Scanner on the file
int rows = fileScan.nextInt();
int columns = fileScan.nextInt();

int[][] myArray = new int[rows][columns];
while (fileScan.hasNextLine()) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
myArray[i][j] = fileScan.nextInt();
}
fileScan.nextLine();
}
}
System.out.println("Array of scores:");
//TODO: print the entire array, row by row, using Arrays.toString()
System.out.println(Arrays.deepToString(myArray).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));

System.out.println("Average score of each assignment:");
//TODO: compute and print the average on each assignment
double total = 0;
int assignment = 1;
for (int i = 0; i < myArray[0].length; i++) {
for (int j = 0; j < myArray.length; j++) {
total += myArray[j][i];
}
System.out.println("Assignment #" + assignment++ + " Average = " + (total / 3));
total = 0;
}
fileScan.close();
keyboard.close();
}
}

关于java - 将 parseInt 更改为 nextInt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58808724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com