gpt4 book ai didi

java - 从文本文件计算成绩

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

我被这个任务困住了。它是从用户输入的文本文件中获取一些成绩,而不是计算最大值、最小值、平均值。问题是每次我尝试编译它时它都会给我这个

St.java:10: readfile(int[],java.lang.String) in St cannot be applied to (int[])
int n = readfile (grades);

源代码:

import java.io.*;

import java.util.Scanner;

public class St

{

public static void main ( String [] a )

{

Scanner kbd = new Scanner(System.in);

int count = 0;
int [] grades = new int [500];
int n = readfile (grades);

System.out.println("What file contains the data?");

String file = kbd.nextLine();

System.out.println("The maximum grade is: " + max(grades,n));
System.out.println("The minimum grade is: " + min(grades,n));
System.out.println("The mean grade is: "+ mean(grades,n));

}
public static double mean(int [] grades,int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
{
sum = sum + grades[i];
}
return sum/n;
}

public static int readfile (int [] grades, String file)
{
int count = 0;
try
{
Scanner f = new Scanner (new File (file)); // name of file
while (f.hasNext()) // checks if there is more input in the file
{
grades[count]=f.nextInt (); // grabs the next piece of input
count++; // moves onto the next piece of input (increasing count)

}
}
catch (IOException e)
{
System.err.println (e);
System.exit(0);
}
return count;


}
public static void writeArray (int [] grades , int n)
{
for (int i = 0; i < n; i++)
{
System.out.println (grades[i]);
}
}
public static int max(int [] y,int m)
{
int mx = 0;
for(int i = 0; i < m; i++)
{
if(y[i] > mx)
{
mx = y[i];
}
}
return mx;
}

public static int min(int [] grades,int n)
{
int mn = 0;

for(int i = 0; i < n; i++)
{
if(grades[i] < mn)
{
mn = grades[i];
}
}
return mn;
}

}

最佳答案

您正在尝试通过

int n = readfile (grades);

但是您创建的函数:

public static int readfile (int [] grades, String file)

要求第二个变量作为字符串

您需要更改线路:

String fileName= YOUR_FILE_NAME;
int n = readfile (grades,fileName);

关于java - 从文本文件计算成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14671197/

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