将十进制值赋给java中的字符串

时间:2017-02-04 20:39:28

标签: java if-statement double cannot-find-symbol

我正在开发一个GPA计算器,用户可以在其中输入字母值,如A,B +,D-等,但我无法将十进制值分配给字母等级。我觉得我没有正确编写代码。我使用if-else语句来检查用户输入值,然后分配一个double以使其与十进制值匹配。我也不确定如何将GPA计算的公式合并到我的if-else语句中。

我得到的最常见的错误是 错误:(126,24)java:找不到符号   符号:变量gpa1   地点:GPA级

import java.util.Scanner;

public class GPA {
    public static void main(String[] args) {

    Scanner input1 = new Scanner(System.in);
    System.out.println("What is your grade in your first class?");
    String course1 = input1.next();


    Scanner input2 = new Scanner(System.in);
    System.out.println("What is the grade in your second class?");
    String course2 = input2.next();


    Scanner input3 = new Scanner(System.in);
    System.out.println("What is the grade in your third class?");
    String course3 = input3.next();


    Scanner input4 = new Scanner(System.in);
    System.out.println("What is the grade in your fourth class?");
    String course4 = input4.next();


    System.out.println("Your grade in your first class was "+ course1);
    System.out.println("your grade in your second class was "+ course2);
    System.out.println("Your grade in your third class was "+ course3);
    System.out.println("Your grade in your fourth class was "+ course4);

        //double A = 4.00;
        //double Aminus = 3.70;
        //double Bplus = 3.33;
        //double B =  3.00;
        //double Bminus = 2.70;
        //double Cplus = 2.30;
        //double C = 2.00;
        //double Cminus = 1.70;
        //double Dplus = 1.30;
        //double D = 1.00;
        //double Dminus = 0.70;


    if (course1.equals("A")) {
        double grade1;
        grade1 = 4.00;
        double gpa1 = grade1;
    }else if (course1.equals("A-")) {
        double grade1;
        grade1 = 3.70;
        double gpa1 = grade1;
    }else if (course1.equals("B+")) {
        double grade1;
        grade1 = 3.33;
        double gpa1 = grade1;
    }else if (course1.equals("B")) {
        double grade1;
        grade1 = 3.00;
        double gpa1 = grade1;
    }else if (course1.equals("B-")) {
        double grade1;
        grade1 = 2.70;
        double gpa1 = grade1;
    }else if (course1.equals("C+")) {
        double grade1;
        grade1 = 2.30;
        double gpa1 = grade1;
    }else if (course1.equals("C")) {
        double grade1;
        grade1 = 2.00;
        double gpa1 = grade1;
    }else if (course1.equals("C-")) {
        double grade1;
        grade1 = 1.70;
        double gpa1 = grade1;
    }else if (course1.equals("D+")) {
        double grade1;
        grade1 = 1.30;
        double gpa1 = grade1;
    }else if (course1.equals("D")) {
        double grade1;
        grade1 = 1.00;
        double gpa1 = grade1;
    }else if (course1.equals("D-")) {
        double grade1;
        grade1 = 0.70;
        double gpa1 = grade1;
    }else if (course2.equals("A")) {
        double grade2;
        grade2 = 4.00;
    }else if (course2.equals("A-")) {
        double grade2;
        grade2 = 3.70;
    }else if (course2.equals("B+")) {
        double grade2;
        grade2 = 3.33;
    }else if (course2.equals("B")) {
        double grade2;
        grade2 = 3.00;
    }else if (course2.equals("B-")) {
        double grade2;
        grade2 = 2.70;
    }else if (course2.equals("C+")) {
        double grade2;
        grade2 = 2.30;
    }else if (course2.equals("C")) {
        double grade2;
        grade2 = 2.00;
    }else if (course2.equals("C-")) {
        double grade2;
        grade2 = 1.70;
    }else if (course2.equals("D+")) {
        double grade2;
        grade2 = 1.30;
    }else if (course2.equals("D")) {
        double grade2;
        grade2 = 1.00;
    }else if (course2.equals("D-")) {
        double grade2;
        grade2 = 0.70;
    }else {
        System.out.println("incorrect");

    System.out.println(gpa1);











    }
    }
}

1 个答案:

答案 0 :(得分:0)

gpa1不在您尝试访问它以进行打印的范围内。

在if / else语句之前定义它,如下所示:

double gpa1 = 0.0;

if (course1.equals("A")) {
        double grade1;
        grade1 = 4.00;
        gpa1 = grade1;
} else if (course1.equals("A-")) {
        double grade1;
        grade1 = 3.70;
        gpa1 = grade1;
}