gpt4 book ai didi

Java 读取文件/扫描仪

转载 作者:行者123 更新时间:2023-12-01 14:37:05 28 4
gpt4 key购买 nike

我是java初学者,我正在努力让这段代码正常工作。我正在尝试读取 CSV 文件,然后使用该数据计算平均值,然后返回平均值的最高最低值和平均值的摘要。

输入如下所示:

Alicia Marks,89,90,100,95,75,85,94,100,90,92

Bobby Ricks,98,79,87,79,9,98,7,19,98,78

输出应如下所示(数字不正确,仅作为示例):

Alicia Marks 85.5 B

Bobby Ricks 90.0 A-

...

High 98.2

Low 56.5

Average 78.3333

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

public class Test
{

public static void main(String[] args)
{
Test x = new Test();
x.high();
}

public void high()
{
File file2 = new File("scores.csv");
try
{
Scanner scanner = new Scanner(file2);
while (scanner.hasNextLine())
{
String line2 = scanner.nextLine();
String[] value2 = line2.split(",");

// Converts String Array into Double Array
double[] score = new double[value2.length];
for (int i = 1; i < value2.length; i++)
{
score[i] = Double.parseDouble(value2[i]);
}

// Finds the sum and then the average, adds it to a array List
ArrayList<Double> av;
av = new ArrayList<Double>();
double sumNum = 0.0;
for (double i : score)
{
sumNum += i;
}
double aver = sumNum / 10;
av.add(aver);

double max = 0, min = 100;
for (int a = 0; a < av.size(); a++)
{
double s = av.get(a);
max = Math.max(max, s);
min = Math.min(min, s);
aver += s;
}

System.out.println("High " + max + "");
System.out.println("Low " + min + "");
System.out.println("Average " + aver / av.size() + "");

}
scanner.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}

我认为我遇到的问题是 while 循环,但我不太确定。任何解决此问题的帮助将不胜感激。

最佳答案

读取成绩的函数中存在一个错误。您应该按以下方式修复它:

// Converts String Array into Double Array
double[] score = new double[value2.length - 1];
for (int i = 1; i < value2.length; i++) {
score[i - 1] = Double.parseDouble(value2[i]);
}

据我所知,您需要所有成绩的最大值、最小值和平均值。那么你的代码应该是这样的:

package it.unitn.uvq.antonio.processor;

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

public class Test {

public static void main(String[] args) {
Test x = new Test();
x.high();
}

public void high() {
File file2 = new File("/home/antonio/Scrivania/scores.csv");
try {
Scanner scanner = new Scanner(file2);
ArrayList<Double> avgs = new ArrayList<>();
double max = 0, min = 100;
while (scanner.hasNextLine()) {
String line2 = scanner.nextLine();
String[] value2 = line2.split(",");

// Converts String Array into Double Array
double[] score = new double[value2.length - 1];
for (int i = 1; i < value2.length; i++) {
score[i - 1] = Double.parseDouble(value2[i]);
}

// Finds the sum and then the average, adds it to a array List
double sumNum = 0.0;
for (double i : score) {
sumNum += i;
min = Math.min(min, i);
max = Math.max(max, i);
}
double avg = sumNum / 10;
avgs.add(avg);

System.out.println(avg);
}
scanner.close();
double avgsSum = .0;
for (double avg : avgs) {
avgsSum += avg;
}
System.out.println("High " + max + "");
System.out.println("Low " + min + "");
System.out.println("Average " + avgsSum / avgs.size() + "");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

关于Java 读取文件/扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16388433/

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