我是 Java 新手,我正在尝试编写一个程序,要求用户输入仅包含数字的 txt 文件的名称,程序将输出文件中数字的总和、平均值、最大值和最小值. 我已经编写了大部分程序,但是我一直试图找到值的最大值和最小值。您可以提供的任何信息都会有所帮助,如果我不够清楚,我可以尝试详细说明。到目前为止,我的代码是:
public class NumberFile{
public static void main(String[] args){
boolean goodName = false;
int currentNumber, sum = 0, numberCount=0;
Scanner numberFile = null;
FileReader infile;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the file you wish to import: ");
String fileName = input.nextLine();
while (!goodName){
try{
infile = new FileReader(fileName);
numberFile = new Scanner(infile);
goodName = true;
}
catch (IOException e){
System.out.println("invalid file name, please enter another name");
fileName = input.nextLine();
}
}
while (numberFile.hasNextInt()){
currentNumber = numberFile.nextInt();
sum+=currentNumber;
numberCount++;
}
System.out.println("The sum of the numbers is " +sum);
System.out.println("The average of the numbers is " + ((double) sum)/numberCount);
} // end main
} // end class