0

java方法statithing,但我似乎无法让我在第56行的while语句正确调用我的方法。有什么我做错了吗?我对 Java 很陌生,所以任何形式的帮助都将不胜感激!提前致谢!这是我的代码:

import java.util.*;
import javax.swing.*;
import java.io.*;

public class GradeCalculator { 
    static String fileInput;
    static double totalGrade;
    static int scoreCount= 0;
    static double classAverage;
    static double score;
    static double testScore;
    static double averageScore;
    static int numberOfStudents = 0;
    static char letterGrade;
    static String fileOutput;
    static String nameOfStudent;
    static int numberCount;
    static int numberCalculatedStudents;
    static double average = 0;

    public static void main (String[] args) throws FileNotFoundException {
    //Welcome   

    JOptionPane.showMessageDialog(null,"Welcome to the Grade Calculator!  This program will\n" +
                                                " calculate the average percentage of 5 test scores that\n"+
                                                " are given in a given file once these scores are\n"+
                                                " averaged it will give the student a letter grade.","Welcome!",JOptionPane.PLAIN_MESSAGE);
    fileInput = JOptionPane.showInputDialog(null, "Please enter the name of the input file you wish to use for this program."
                                            ,"Input File",JOptionPane.PLAIN_MESSAGE);
    fileOutput = JOptionPane.showInputDialog(null, "Please enter the name of the output file you wish to use for this program."
                                            ,"Output File",JOptionPane.PLAIN_MESSAGE);
    //preparing text files
    PrintWriter outFile = new PrintWriter (fileOutput);                                         
    File inFile = new File (fileInput);

    Scanner reader = new Scanner(new FileReader(fileInput));
    outFile.println("Student Name   Test1   Test2   Test3   Test4   Test5   Average Grade");

    while(reader.hasNextLine()) {
        nameOfStudent = reader.next();
        outFile.printf("%n%n%s",nameOfStudent);
        numberOfStudents++;
        score = reader.nextDouble();
        calculateAverage(score);
        calculateGrade(averageScore);
        outFile.printf("                                %.2f   ", averageScore);
        outFile.println("                                                               "+letterGrade);
    }
    classAverage = classAverage/numberCalculatedStudents;       
    outFile.print("\n\n\n\n\n\n\n\n\n\n\n\nClass average for "+ numberCalculatedStudents + "of" + numberOfStudents + "is" + classAverage);
    JOptionPane.showMessageDialog(null,"The report has successfully been completed and written into the file of " + fileOutput +"."
                                                    +" The class average is " + classAverage + ". Please go to the output file for the complete report.");  
    outFile.close();
    }

    public static void calculateAverage(double score) throws FileNotFoundException {
        Scanner reader = new Scanner(new FileReader(fileInput));
        PrintWriter outFile = new PrintWriter (fileOutput);
        while (reader.hasNextDouble() && numberCount <= 5 ) {
            score = reader.nextDouble();
            numberCount++;
        if (score >= 0 & score <= 100) {
                outFile.printf("%10.2f",score);
            scoreCount++;
            average = score + average;
        }
        else
            outFile.printf("                **%.2f",score);
        }
        if (average!=0){
            numberCalculatedStudents++; 
            average = average/scoreCount;
            averageScore = average;
            classAverage = average + classAverage;
            }

            average = 0;
    }

    public static char calculateGrade (double averageScore) {

        if (averageScore >= 90 && averageScore <= 100)
            letterGrade = 'A';
        else if (averageScore < 90 && averageScore >= 80)
            letterGrade = 'B';
        else if (averageScore < 80 && averageScore >= 70)
            letterGrade = 'C';
        else if (averageScore < 70 && averageScore >= 60)
            letterGrade = 'D';
        else if (averageScore < 60 && averageScore >= 0)
            letterGrade = 'F';  
        else 
            letterGrade =' ';

        return letterGrade;
     }  
}
4

1 回答 1

0

在不知道问题出在哪一行的情况下,我突然想到了两个问题:

在 while 循环的顶部:

   if (score >= 0 & score <= 100)
    { outFile.printf("%10.2f",score);
        scoreCount++;
        average = score + average;
    }
    else
        outFile.printf("                **%.2f",score);}

else 语句后有一个右括号 ( }),但没有左括号。因此,该右括号看起来像是在您想要它之前退出了 while 循环。

其次,看起来您正试图在CalculateGrade 方法中返回一些东西(即一个字符),但是您在其上指定了一个返回类型为void,这意味着即使您有一个return 语句,当您称它为。你没有显示你在哪里调用那个方法,所以我不能确定这会导致问题,但它肯定是可疑的。您似乎想使用:

 public static char calculateAverage(double score) throws FileNotFoundException{

代替public static void calculateAverage(double score)...

另外,所有这些方法都是静态的有什么原因吗?你知道制作静态的东西是什么意思吗?

编辑(根据您的评论):

不,创建一个变量static使其成为“类变量”,这意味着该类的所有对象只存在其中一个。为了显示:

如果你有这样的课程:

class test {
static int id;
}

然后运行以下代码:

    test t1 = new test();
    test t2 = new test();

    t1.id = 4;
    t2.id = 8;

    System.out.println(t1.id);

它将打印 8。这是因为,因为 id 是一个static变量,在类的任何对象上更改它都会导致它为类的每个其他对象而更改。

这与类的每个对象都存在的“实例变量”相反。要创建id实例变量,只需删除static关键字。如果这样做并运行相同的代码,它将打印 4,因为更改 t2 的实例变量对 t1 没有影响。

有道理?

于 2013-04-18T09:40:27.107 回答