gpt4 book ai didi

java - 错误 :Issue with reading text file in class I created

转载 作者:行者123 更新时间:2023-11-29 05:33:17 27 4
gpt4 key购买 nike

当我运行我的程序 (BaseballStatsClient) 时,我收到错误:无法找到球队名称、最大值和最小值的符号

更新:

我现在遇到一个问题,我认为是文本文件的读取,当我运行程序时,团队名称的输出值为 tars.txt,最大值和最小值均为 0.0。我认为在第一个代码中我没有正确读取文本文件

文本文件:

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

这是我创建的类文件,我认为它读取文本文件的方式存在问题:

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

public class BaseballStats {

private String fileName;
private String teamName;
private double [] battingAverage;


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;
}
public double spread( ) {
//returns the difference between the highest and lowest batting averages
}
public int goodPlayers( ) {
//returns the number of players with an average higher than .300
}
public String toString( ) {
// returns a String containing the team name followed by all the batting averages formatted to three decimal places.
}
}

这是我正在运行的程序:

import java.util.Scanner;

public class BaseballStatsClient {
public static void main( String [] args ) {
Scanner scan = new Scanner (System.in );
System.out.println("What is the name of the file you would like to find the statistics of?");
String fileName = scan.next( );

BaseballStats newTeam = new BaseballStats(fileName);
System.out.println("The " + newTeam.getTeamName() + " statistics are:");
System.out.println("Highest Batting Average: " + newTeam.findMaxAverage( ));
System.out.println("Lowest Batting Average: " + newTeam.findMinAverage( ));
}
}

最佳答案

newTeam.getTeamName( );
System.out.println("The " + teamName + " statistics are:");
newTeam.findMaxAverage( );
System.out.println("Highest Batting Average: " + max);
newTeam.findMinAverage( );
System.out.println("Lowest Batting Average: " +min);

您尚未定义定义它们并尝试打印它们。

用返回值定义变量。

喜欢

   String teamName =newTeam.getTeamName( );
System.out.println("The " + teamName + " statistics are:");

对于剩余的 minmax 变量也是如此。

而且你可以直接做

 System.out.println("The " + newTeam.getTeamName() + " statistics are:");

注意newTeam,可能null

关于java - 错误 :Issue with reading text file in class I created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369792/

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