循环时不会执行成绩计算器

时间:2014-03-07 02:16:14

标签: java do-while

我有一个班级作业,要求我们从用户那里获取输入,删除最低的作业成绩,然后再拿几个等级,计算平均成绩。我已经做了所有这些,但最后一步是我们必须询问用户是否要为另一个学生重复该程序。我所拥有的do while循环不会执行。

import java.util.Scanner;

public class CalculateGrades{
public static void main(String [] args){
    Scanner sc = new Scanner (System.in);

    double score;
    double sum=0;
    double lowest=100;

    do{
        //ASSIGNMENT GRADES
        for(int i=1;i<=5;i++){
            System.out.println("enter grade for assignment " +i);
            score=sc.nextDouble();
            sum=sum+score;

            if(score<lowest){
                lowest=score;
            }
        }
        sum=sum-lowest;
        double assignmentAvg= sum/4;
        System.out.println("After the lowest grade is dropped, your assignment average is " + assignmentAvg);

        //EXAM GRADES
        System.out.println("enter grade for exam one");
        double exam1=sc.nextDouble();
        System.out.println("enter grade for exam two");
        double exam2=sc.nextDouble();

        //LAB GRADE
        System.out.println("enter grade for lab");
        double lab= sc.nextDouble();

        //FINAL EXAM GRADE
        System.out.println("enter grade for final exam");
        double Final=sc.nextDouble();

        //AVERAGE
        double avg= (assignmentAvg*.2)+(exam1*.15)+(exam2*.2)+(lab*.2)+(Final*.25);

        if(avg >=90){
            System.out.println("Your weighted average is " + avg + " and your letter grade is A");
        }
        else if(avg >=80){
            System.out.println("Your weighted average is " + avg + " and your letter grade is B");
        }
        else if(avg >=70){
            System.out.println("Your weighted average is " + avg + " and your letter grade is C");
        }
        else if(avg >=60){
            System.out.println("Your weighted average is " + avg + " and your letter grade is D");
        }
        else{
            System.out.println("Your weighted average is " + avg + " and your letter grade is F");
        }

        System.out.println("Would you like to calculate for another student? Press 1 for yes, or any key to terminate");
        int again= sc.nextInt();

    }while(again==1);

    // for next student reset sum
}
}

1 个答案:

答案 0 :(得分:0)

您的again变量超出了范围。检查this。修复是在循环之前声明again