如何使用方法而不会得到相同的结果

时间:2017-04-28 06:37:26

标签: java

我的程序从文件中取得成绩,并询问用户是否需要字母等级或数字等级。但是我还要说,如果等级通过或失败,我做了一个使用switch语句的方法。

问题在于它只为所有这些打印传递。因为它只测试一年级然后打印出字符串。我将如何测试每个等级并打印相应的通过或失败?

    public class Grade_Class {

    String letterGrade;

    Grade_Class() // Default constructor
    {

    }

    public boolean pass(String letterGrade) {
        boolean pass = true;

        switch (letterGrade) {
        case "A":
        case "B":
        case "C":
        case "D":
            pass = true;
            break;
        case "F":
            pass = false;
            break;

        }
        return pass;

    }

    public String getLetterGrade(double testScore) // gets letter value of grade
    {
        String letterGrade = "";
        if (testScore >= 95) {
            letterGrade = "A+";
        } else if (testScore <= 94.9 && testScore >= 90) {
            letterGrade = "A";
        } else if (testScore <= 89.9 && testScore >= 85) {
            letterGrade = "B+";
        } else if (testScore <= 84.9 && testScore >= 80) {
            letterGrade = "B";
        } else if (testScore <= 79.9 && testScore >= 75) {
            letterGrade = "C+";
        } else if (testScore <= 74.9 && testScore >= 70) {
            letterGrade = "C";
        } else if (testScore <= 69.9 && testScore >= 65) {
            letterGrade = "D";
        } else if (testScore <= 64.9) {
            letterGrade = "F";
        }

        return letterGrade;
    }
}

    import javax.swing.JOptionPane;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class test {

    public static void main(String[] args) throws IOException {
        Grade_Class obj1 = new Grade_Class(); // creates obj of Grade_Class

        String name = "";
        String option = "";
        int userOpt = 0;

        File inputFile = new File("grades.txt");
        Scanner myInput = new Scanner(inputFile);
        double grades[] = new double[12];
        String passOrFail[] = new String[12];
        double test = 0;
        String letterGrade = "";
        String passFail = "";

        FileWriter fw = new FileWriter("LetterGrade.txt");
        PrintWriter outputFile = new PrintWriter(fw);

        name = JOptionPane.showInputDialog("Enter students name:");
        option = JOptionPane
                .showInputDialog("Pick an option \n 1: print out grade letter \n 2: print out number grade");
        userOpt = Integer.parseInt(option);
        System.out.println("Student name:" + name);

        for (int i = 1; i < 12; i++) {
            test = myInput.nextDouble();
            grades[i] = test;
            if (userOpt == 1) {

                /*
                 * if (obj1.pass(letterGrade)) { passFail = "pass"; } else {
                 * passFail = "fail"; }
                 */

                System.out.println("Test " + i + ": " + obj1.getLetterGrade(test) + " " + passFail);
                DecimalFormat df = new DecimalFormat("###.00");
                Double average1 = Double.parseDouble(df.format(test));
                outputFile.println("Test " + i + ": " + obj1.getLetterGrade(test) + " " + average1 + "%");
            } else if (userOpt == 2) {
                DecimalFormat df = new DecimalFormat("###.00");
                Double average1 = Double.parseDouble(df.format(test));

                /*
                 * if (obj1.pass(letterGrade) == true) { passFail = "pass"; }
                 * else { passFail = "fail"; }
                 */
                System.out.println("Test " + i + ": " + average1 + "%" + " " + passFail);
                outputFile.println("Test " + i + ": " + obj1.getLetterGrade(test) + " " + average1 + "%");

            }

        }

        myInput.close();
        outputFile.close();

    }
}

1 个答案:

答案 0 :(得分:1)

对于这么简单的错误,代码太多了......

if (obj1.pass(letterGrade)) { 
    passFail = "pass"; 
} else {
    passFail = "fail"; 
}

您永远不会将letterGrade设置为""以外的任何内容。 只需将if (obj1.pass(letterGrade))替换为if (obj1.pass(obj1.getLetterGrade(test))),就可以了。

意思是有效。你应该对你的代码风格和一般架构真的做些什么,但无论如何,这是你犯错误的地方。