为什么我的程序不能调用我的方法?

时间:2013-04-18 09:30:17

标签: java static-methods

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;
     }  
}

1 个答案:

答案 0 :(得分:0)

如果不知道问题出在哪一行,我就会跳出两个问题:

在while循环的顶部:

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

在else语句后面有一个小括号(}),但没有开括号。因此,在您想要它之前,关闭括号看起来像是退出while循环。

其次,看起来你试图在CalculateGrade方法中返回一些东西(即一个char),但是你已经在它上面指定了一个返回类型的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没有影响。

有意义吗?