【发布时间】:2023-03-30 01:50:01
【问题描述】:
所以当我尝试保存和编译时,一切正常,直到我运行它。我的数组语法似乎有问题。有人可以帮我找到它吗?当我运行这个程序时,grades() 方法会输出 "AAA" 。我在这个程序中尝试做的是从 txt 文件中读取文本并列出每一行,输出学生姓名和分数。现在在 Grades() 方法中,我试图输出计算每个学生成绩的字母成绩,并使其进入循环,直到读取最后一个分数。
public class ReadData
{
private static String[] names = new String[3];
private static int line;
private static int[] scores = new int[3];
private static float mean;
private static double stdDeviation;
public static void readData() throws FileNotFoundException
{
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
int l = 0;
// float sum = 0 ;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String [] words = line.split("\t");
names[l] = words[0];
scores[l] = Integer.parseInt(words[1]);
// sum+=scores[l];
System.out.println(" name: " + names[l] + ", score: " + scores[l]);
l++;
}
// System.out.println(scores[0]+ " " + scores[1]+ " " + scores[2]);
}
public static void fndMean()
{
float mean = ((25+65+89)/3);
System.out.println(" The mean is: " + mean);
}
public static void fndStandard() throws FileNotFoundException
{
double stdDeviation = Math.sqrt(((Math.pow(25-59, 2)+(Math.pow(65-59,2))+
(Math.pow(89-59, 2))))/3);
System.out.println("The Standard Deviation is: " + stdDeviation);
}
评分法
public static void grades()
{
for(int i = 0; i < (scores.length); i++)
{
if(mean + stdDeviation <= scores[i])
{
System.out.print("A");
}
else if( (scores[i] >= mean+(stdDeviation/3)) &&
(mean +stdDeviation)> scores[i])
{
System.out.print("B");
}
else if( (scores[i] >= mean-(stdDeviation/3)) &&
(mean +(stdDeviation/3))> scores[i])
{
System.out.print("C");
}
else if( (scores[i] >= mean-(stdDeviation)) &&
(mean - (stdDeviation/3))> scores[i])
{
System.out.print("D");
}
else
{
System.out.println("F");
}
}
}
【问题讨论】:
-
对于未来读者对
grades()的位置感到困惑,请向下滚动。 -
单步执行您的代码,看起来您的问题是您一遍又一遍地为数组中的第二项分配相同的值...
-
还有:
if(mean + stdDeviation <= scores[i])。不需要在else中重复相反的条件...已经是else了! -
欢迎来到 Stack Overflow!看来您需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果之后仍有问题,请随时回来提供更多详细信息。
标签: java arrays if-statement for-loop