将平均分数转换为字母等级

时间:2014-11-14 19:02:26

标签: java average

我正在设计一个程序,使用下面的评分系统,使用4个科目中4个分数的平均成绩对学生进行评分:

80-100 = A 
70-79  = B 
60-69  = C 
50-59  = D 
40-49  = E
0 -39  = F

我坚持将平均值转换为字母等级的最后一部分。这是我正在使用的代码:

package practicals;
import java.util.Scanner;
public class GradeSystem {

    public static void main(String[] args) {
        // variable declaration 

        String name;
        int mark1, mark2, mark3, mark4;
        int grade = 0;
        int A, B, C, D, F;

        double average;
        // input
        Scanner input = new Scanner(System.in);
        System.out.println("Enter Your Name");
        name = input.nextLine();

        Scanner number = new Scanner(System.in);
        System.out.println("Enter Your Registration Number");
        name = input.nextLine();

        Scanner marks = new Scanner(System.in);
        System.out.println("Enter The Student Marks from The Four Subjects");
        mark1 = input.nextInt();
        mark2 = input.nextInt();
        mark3 = input.nextInt();
        mark4 = input.nextInt();

        input.close();
        average = (double) (mark1 + mark2 + mark3 + mark4) / 4;
        System.out.println("Your average from last semester is " + average);

        if (grade >= 80 && grade <= 100) {
            System.out.println("Your Grade is A");
        } else if (grade >= 70 && grade <= 79) {
            System.out.println("Your Grade is B");
        } else if (grade >= 60 && grade <= 69) {
            System.out.println("Your Grade is C");
        } else if (grade >= 50 && grade <= 59) {
            System.out.println("Your Grade is D");
        } else if (grade >= 40 && grade <= 49) {
            System.out.println("Your Grade is E");
        } else if (grade >= 0 && grade <= 39) {
            System.out.println("Your Grade is F");
        }
    }
}

4 个答案:

答案 0 :(得分:1)

您正在计算平均变量的平均值,并将if-else条件中的结果与您的成绩变量进行比较。此外,您将在您的名称变量中保存注册号。顺便说一句,您不需要为每次阅读创建新的扫描仪。试试这个代码:

public static void main(String[] args) {
    // variable declaration 

    String name, registrationNumber;
    int mark1, mark2, mark3, mark4;

    double average;

    // input

    Scanner input = new Scanner (System.in);
    System.out.println("Enter Your Name");
    name = input.nextLine();

    System.out.println("Enter Your Registration Number");
    registrationNumber = input.nextLine();

    System.out.println("Enter The Student Marks from The Four Subjects");
    mark1 = input.nextInt();
    mark2 = input.nextInt();
    mark3 = input.nextInt();
    mark4 = input.nextInt();

    input.close();

    average = (double) (mark1+mark2+mark3+mark4)/4;

       System.out.println("Your average from last semester is " + average);


     if (average>= 80 && average <= 100)
        System.out.println("Your Grade is A");
    else if(average >=70 && average <=79)
        System.out.println("Your Grade is B");
    else if(average>=60 && average <=69)
        System.out.println("Your Grade is C");
    else if(average>=50 && average <=59)
        System.out.println("Your Grade is D");
    else if(average>= 40 && average <=49)
        System.out.println("Your Grade is E");
    else if(average>= 0 && average <=39)
        System.out.println("Your Grade is F");

}

答案 1 :(得分:0)

您已将成绩初始化为0且从未更新过它 你需要替换

  

等级

  

平均

在所有if语句中

if (average >= 80 && average <= 100)

答案 2 :(得分:0)

您不需要grade变量,因为您正在使用average进行计算 为了提高代码效率,请不要比较边缘情况。

if (average >= 80) {
    System.out.println("Your Grade is A");
} else if (average >= 70 && grade <= 79) {
    System.out.println("Your Grade is B");
} else if (average >= 60 && grade <= 69) {
    System.out.println("Your Grade is C");
} else if (average >= 50 && grade <= 59) {
    System.out.println("Your Grade is D");
} else if (average >= 40 && grade <= 49) {
    System.out.println("Your Grade is E");
} else {
    System.out.println("Your Grade is F");
}

而且您也不需要声明以下变量

int A, B, C, D, F;

答案 3 :(得分:0)

你在这里做错的是在grade进行比较,而你真正想做的就是比较平均值。您应该更改if / elseif块以使用平均值。

我还建议将其更改为仅检查间隔,如下所示:

if(average >= 80){
   System.out.println("Your grade is A");
} else if(average >= 70){
   System.out.println("Your grade is B");
} else if(average >=60) {
   System.out.println("Your grade is C");
} else if(average >= 50){
   System.out.println("Your grade is D");
} else if(average >= 40){
   System.out.println("Your grade is E");
} else{
   System.out.println("Your grade is F");
}

这看起来更清洁,更符合逻辑。唯一没有处理的是100以上的等级(将被视为A)和低于0的等级(将被视为F),因此您可能希望将初始块更改为:

if(average < 0 || average > 100){
   System.out.println("Invalid average.");
}