可能无法初始化局部变量

时间:2016-09-23 01:44:48

标签: java

我正在尝试制作成绩计算器。除了程序没有正确处理条件以输出正确的字母等级外,一切都符合规范。

对此有何帮助?

import java.util.Scanner;

public class GradeCalculator {
    public static void main(String args []) {
        String name;
        String major;
        int attendance;
        int homework;
        int project1;
        int project2;
        int midterm;
        int finalexam;
        int extra;
        String finalgrade;
        double attweight = 0.05;
        double hwweight = 0.35;
        double project1weight = 0.075;
        double project2weight = 0.075;
        double midtermweight = 0.20;
        double finalweight = 0.25;
        double totalgrade;
        Scanner in = new Scanner(System.in);
        System.out.println("What is your name? ");
        name = in.nextLine();
        System.out.println("What is your major? ");
        major = in.nextLine();
        System.out.println("What is your attendance grade (out of 100)? ");
        attendance = in.nextInt();
        System.out.println("What is your homework grade (out of 100)? ");
        homework = in.nextInt();
        System.out.println("What is your project 1 grade? ");
        project1 = in.nextInt();
        System.out.println("What is your project 2 grade? ");
        project2 = in.nextInt();
        System.out.println("What is your midterm grade? ");
        midterm = in.nextInt();
        System.out.println("What is your final grade? ");
        finalexam = in.nextInt();
        if(attendance > 100 || homework > 100 || project1 > 100 || project2 >100 || midterm > 100 || finalexam > 100) {
            System.out.println("Your grade cannot exceed 100");
        }
        totalgrade = ((attendance * attweight) + (homework * hwweight) + (project1 * project1weight) + (project2 * project2weight) + (midterm * midtermweight) + (finalexam * finalweight));
        if(totalgrade > 93 && totalgrade < 100){
            finalgrade = "A";
        }
        if(totalgrade > 89 && totalgrade < 93){
            finalgrade = "A-";
        }
        if(totalgrade > 86 && totalgrade < 90){
            finalgrade = "B+";
        }
        if(totalgrade > 82 && totalgrade < 87){
            finalgrade = "B";
        }
        if(totalgrade > 79 && totalgrade < 83){
            finalgrade = "B-";
        }
        if(totalgrade > 76 && totalgrade < 80){
            finalgrade = "C+";
        }
        if(totalgrade > 72 && totalgrade < 77){
            finalgrade = "C";
        }
        if(totalgrade > 69 && totalgrade < 73){
            finalgrade = "C-";
        }
        if(totalgrade > 59 && totalgrade < 70){
            finalgrade = "D";
        }
        if(totalgrade < 60){
            finalgrade = "F";
        }

        System.out.println("Your name is " + name + " and your major is " + major + ". Your attendance grade is " + attendance + ", your homework grade is " + homework + ", your project one grade is " + project1 + ", your project two grade is " + project2 + ", your midterm grade is " + midterm + ", your final exam grade is" + finalexam + ". Your total grade is " + totalgrade + " and your letter grade is " + finalgrade);
    }
}

错误:

  

本地变量finalgrade可能尚未初始化

1 个答案:

答案 0 :(得分:1)

编辑:精化

你已经告诉我们这个问题了。您需要初始化finalgrade。你在条件语句中初始化它,但是当你尝试读取输出中的变量值时,不能保证这种情况发生(因为初始化只在满足条件时才会发生)所以编译器会生气。一个简单的解决方法是开始将finalgrade设置为空字符串。

String finalgrade = "";

另外,请查看条件语句。你排除了93,也许还有其他数字。最好使用&#39;否则如果&#39;,在第一个&#39;之后&#39;言。