gpt4 book ai didi

java - 创建类文件时出现问题

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

我正在创建一个程序来读取和输出棒球统计数据。我正在尝试创建一个类文件来读取 txt 文件,该文件由球队名称后跟 20 个击球平均值组成。

我需要帮助来确保我使用的代码正确读取文本文件以及如何创建最后 3 个方法。

到目前为止的代码:

import java.util.Scanner;

public class BaseballStats {

private String fileName;
private String teamName;
private double[] battingAverage = new double[20];

public BaseballStats(String fileName) {
this.fileName = fileName;
boolean firstLine = true;
Scanner input = new Scanner(fileName);
while (input.hasNextLine()) {
String line = input.nextLine();
if (firstLine) {
setTeamName(line);
firstLine = false;
continue;
}
int i = 0;
while (input.hasNext()) {
battingAverage[i] = input.nextDouble();
i++;
}
}
}

public String getTeamName() {
return teamName;
}

public void setTeamName(String newTeamName) {
teamName = newTeamName;
}

public double findMaxAverage() {
double max = battingAverage[0];
for (int i = 1; i < battingAverage.length; i++) {
if (battingAverage[i] > max)
max = battingAverage[i];
}
return max;
}

public double findMinAverage() {
double min = battingAverage[0];
for (int i = 1; i < battingAverage.length; i++) {
if (battingAverage[i] < min)
min = battingAverage[i];
}
return min;
}
}

文本文件:

Tars 
0.592
0.427
0.194
0.445
0.127
0.483
0.352
0.190
0.335
0.207
0.116
0.387
0.243
0.225
0.401
0.382
0.556
0.319
0.475
0.279

公开双价( )

返回最高和最低击球率之间的差异

public int goodPlayers( )

返回平均高于 0.300 的玩家数量

公共(public)字符串toString()

返回一个字符串,其中包含球队名称,后跟所有格式化为小数点后三位的击球平均值。

最佳答案

您还没有创建 File 对象,您所做的只是读取字符串文件名

this.fileName = fileName;
boolean firstLine = true;

try {
File file = new File(fileName); // you need to create a File object
Scanner input = new Scanner(file); // then pass that File to the scanner
catch(FileNotFoundException ex) {
ex.printStackTrace())
}

while (input.hasNextLine()) {
...
}

通过您正在做的事情,您正在读取字符串文件名,例如“file.txt”

所以扫描仪只是读取字符串“file.txt”,而不是文件

读取字符串后,就没有任何内容了。因此数组中的所有值都保持为 0。这就是为什么结果始终为 0.0。

编辑:

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

public class BaseBallStats {

private String fileName;
private String teamName;
private double[] battingAverage = new double[20];

public BaseBallStats(String fileName) {
this.fileName = fileName;
boolean firstLine = true;
Scanner input = null;
try {
File file = new File(fileName); // you need to create a File object
input = new Scanner(file); // then pass that File to the scanner
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}

while (input.hasNextLine()) {
String line = input.nextLine();
if (firstLine) {
setTeamName(line);
firstLine = false;
}
int i = 0;
while (input.hasNext()) {
battingAverage[i] = input.nextDouble();
i++;
}
}
}

public String getTeamName() {
return teamName;
}

public void setTeamName(String newTeamName) {
teamName = newTeamName;
}

public double findMaxAverage() {
double max = battingAverage[0];
for (int i = 1; i < battingAverage.length; i++) {
if (battingAverage[i] > max) {
max = battingAverage[i];
}
}
return max;
}

public double findMinAverage() {
double min = battingAverage[0];
for (int i = 1; i < battingAverage.length; i++) {
if (battingAverage[i] < min) {
min = battingAverage[i];
}
}
return min;
}

public static void main(String[] args) {
BaseBallStats stats = new BaseBallStats("file.txt");
System.out.println(stats.findMaxAverage());
System.out.println(stats.findMinAverage());
}

}

关于java - 创建类文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369238/

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