为什么它不返回带有返回类型char的等级?

时间:2017-03-10 21:07:52

标签: java return

我已经开始重新学习Java了。我在显示char数据类型返回值时遇到问题。一切都在工作,期待这一切。它应该能够将等级显示为A B C D,但它是空白的。有人可以帮我找到错误并解决问题吗?有人建议我可以使用System.out.println(“A”)使其工作;在返回'A之前我试过但它不起作用。我在SF搜索但无法找到确切问题的解决方案。提前谢谢。

package score;

import java.util.Scanner;

/**
 *
 * @author rabin
 */
public class Score {
    static double earnedPoints, possiblePoints, pointsPercent;
    static char grade;
    //static String name;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int testNumber;

        String stuName;
        // TODO code application logic here
        System.out.println("How many tests were given?");
        Scanner input1 = new Scanner(System.in);
        testNumber = input1.nextInt();

        System.out.println("Enter Student's Name");
        Scanner nameInput = new Scanner(System.in);
        stuName = nameInput.next();
        if (stuName.contains(" ")){
            System.out.println("This field should not contain spaces");


        }
        pointsEarned(testNumber);
        letterGrade(pointsPercent);
        displayResult(stuName, pointsPercent, grade);
    }
    public static double pointsEarned(int testNumber){

        System.out.println("Enter Points Earned.");
        Scanner pointsInput = new Scanner(System.in);
        earnedPoints = pointsInput.nextDouble();
        System.out.println("Enter Possible Points.");
        Scanner possibleInput = new Scanner(System.in);
        possiblePoints = possibleInput.nextDouble();
        pointsPercent = ((earnedPoints / possiblePoints) * 100);
        return pointsPercent;
    }
    public static char letterGrade(double percentage){
        if (percentage >= 90 && percentage <= 100){
            System.out.println("A");
            return 'A';
        }
        else if (percentage >= 80 && percentage < 90){
            System.out.println("B");
            return 'B';
        }
        else if (percentage >= 70 && percentage < 80){
            System.out.println("C");
            return 'C';
        }
        else if (percentage >= 60 && percentage < 70){
            System.out.println("D");
            return 'D';
        }
        else {
            System.out.println("F");
            return 'F';
        }


    }
    public static void displayResult(String name, double percentage, char grade){
        System.out.println("Result");
        System.out.println("Name: " +name);
        System.out.println("Percent: " +percentage);
        System.out.println("Letter Grade: " +grade);



    }

}

3 个答案:

答案 0 :(得分:3)

您不会将返回的结果放在必须使用的成绩变量中:

grade = letterGrade(pointsPercent);

答案 1 :(得分:1)

问题是您已经调用了适当的方法,但是您没有将其分配给grade变量。

将返回的值分配给grade变量,如下所示:

grade = letterGrade(pointsPercent);

答案 2 :(得分:0)

在您的main计划中,您需要使用static变量来保存方法的return值:

public static void main(String[] args) {
        int testNumber;
        String stuName;
        // TODO code application logic here
        System.out.println("How many tests were given?");
        Scanner input = new Scanner(System.in);
        testNumber = input.nextInt();

        System.out.println("Enter Student's Name");
        //Scanner nameInput = new Scanner(System.in); you don't have to use another scanner variable to read inputs only one is enough
        stuName = input.next();
        if (stuName.contains(" ")){
            System.out.println("This field should not contain spaces");


        }
        pointsPercent = pointsEarned(testNumber); // save the return value of pointsEarned method in pointsPrecent
        grade = letterGrade(pointsPercent); //save the return value of letterGrade method in grade 
        displayResult(stuName, pointsPercent, grade);
    }