Java 从文本文件计算成绩
我被这项任务困住了。它是从用户将输入的文本文件中获取一些分数,而不是计算最大值、最小值和平均值。问题是每次我试图编译它时,它都会给我这个Java 从文本文件计算成绩,java,compiler-errors,Java,Compiler Errors,我被这项任务困住了。它是从用户将输入的文本文件中获取一些分数,而不是计算最大值、最小值和平均值。问题是每次我试图编译它时,它都会给我这个 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 {
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;
}
}
import java.io.*;
导入java.util.Scanner;
公共类街
{
公共静态void main(字符串[]a)
{
扫描仪kbd=新扫描仪(System.in);
整数计数=0;
整数[]等级=新整数[500];
int n=读取文件(等级);
System.out.println(“哪个文件包含数据?”);
字符串文件=kbd.nextLine();
System.out.println(“最大等级为:”+max(等级,n));
System.out.println(“最低等级为:”+min(等级,n));
System.out.println(“平均等级为:”+平均(等级,n));
}
公共静态双平均值(int[]等级,int n)
{
双和=0;
对于(int i=0;imx)
{
mx=y[i];
}
}
返回mx;
}
公共静态最小整数(整数[]等级,整数n)
{
int mn=0;
对于(int i=0;i
您正试图通过考试
int n = readfile (grades);
但是您创建的功能:
public static int readfile (int [] grades, String file)
请求第二个变量作为字符串
您需要更改您的线路:
String fileName= YOUR_FILE_NAME;
int n = readfile (grades,fileName);
您没有使用从用户处获得的输入文件名 更改此项:
int n = readfile (grades);
System.out.println("What file contains the data?");
String file = kbd.nextLine();
进入
因此,您可以从用户处获得文件名,并将其传递给您的方法
System.out.println("What file contains the data?");
String file = kbd.nextLine();
int n = readfile (grades, file);