我的程序没有完全运行

时间:2015-09-30 01:24:01

标签: java if-statement

出于某种原因,我的程序在到达要求用户知道它的考试1分数的部分时会停止。我需要用户能够输入是或否。为什么程序会停止?我需要它才能正常工作。我有所有if-else语句。我可以输入百分比权重,但这就是程序将要执行的所有操作。必须做更多的事情。我的代码远远超出了输入百分比权重。请帮我。

import java.util.Scanner;
public class GradeCalculation {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner grade = new Scanner (System.in);

        // A new scanner must be created. The scanner is essential to this program performing properly. 

        double A = 90-100; 
        double B = 80-89; 
        double C = 70-79; 
        double D = 60-69; 
        double F = 0-59;
        String LetterGrade; 
        String yes; 
        String no;
        double Exam1, Exam2, finalExam, Labs, Projects, Attendance, Quizzes;
        double Exam1Grade, Exam2Grade, finalExamGrade, LabAverage, ProjectsAverage, AttendanceAverage, QuizzesAverage;
        double knownWeight;
        double PercentageWeights;

        // As always, the variables must be declared at the beginning of the program. 


        System.out.print(
                "Grading Scale:\n"+
                "A = 90-100 \n"+
                "B = 80-89 \n"+ 
                "C = 70-79 \n"+ 
                "D = 60-69 \n"+
                "F = 00-59 \n");

        System.out.println("What letter grade do you want to achieve in this course?");
        LetterGrade = grade.next();

        // The user will type the letter grade that it wants in this part. 

        System.out.println("\nEnter Percentage Weights: \t");

        String input = grade.nextLine();

        // The string above is needed when the user enters the exam grades and averages. 

        System.out.print("\n\nExam 1: \t"); 
        Exam1 = grade.nextShort();
        System.out.print("\nExam 2: \t"); 
        Exam2 = grade.nextShort();
        System.out.print("\nFinal Exam: \t");
        finalExam = grade.nextShort();
        System.out.print("\nLabs: \t");
        Labs = grade.nextShort();
        System.out.print("\nProjects: \t");
        Projects = grade.nextShort();
        System.out.print("\nAttendance: \t");
        Attendance = grade.nextShort();
        System.out.print("\nQuizzes: \t");
        Quizzes = grade.nextShort();

        PercentageWeights = (int)(Exam1 + Exam2 + finalExam + Labs + Projects + Attendance + Quizzes);

        // The equation above will provide the sum of the percentage weights. Since the variables in the equation were 
        // originally declared as doubles, I had to put "int" before the actual equation. 


        if (PercentageWeights > 100 || PercentageWeights < 100) { 
            System.out.println("\nWeights do not add up to 100. Program exiting. Have a nice day!");
            System.exit(0);
        }
        else { 
            System.out.println("\nEnter your scores out of a 100: \t");
        }

        // The part above is very important to continue the rest of the program. If the sum of the percentage weights equals 100, 
        // the program will continue to run. If the sum is greater than or less than 100, the program will terminate. 

        System.out.print("\nDo you know your Exam 1 score?");
        if (input.equalsIgnoreCase("yes")) {
            System.out.print("\nScore received on Exam 1: "); 
            Exam1Grade = grade.nextDouble();
        }
        else{ 
            Exam1Grade = 0;
        }

        System.out.print("\nDo you know your Exam 2 score?");
        if (input.equalsIgnoreCase("yes")) { 
            System.out.print("\nScore received on Exam 2: ");
            Exam2Grade = grade.nextDouble();
        }
        else{ 
            Exam2Grade = 0;
        }
        System.out.print("\nDo you know your final exam score?");
        if (input.equalsIgnoreCase("yes")){ 
            System.out.print("\nScore received on final exam: ");
            finalExamGrade = grade.nextDouble();
        }
        else{
            finalExamGrade = 0;
        }
        System.out.print("\nDo you know your lab average?");
        if (input.equalsIgnoreCase("yes")){ 
            System.out.print("\nAverage lab grade: "); 
            LabAverage = grade.nextDouble();
        }
        else{
            LabAverage = 0;
        }
        System.out.print("\nDo you know your project average?");
        if (input.equalsIgnoreCase("yes")){
            System.out.print("\nAverage project grade: "); 
            ProjectsAverage = grade.nextDouble();
        }
        else{
            ProjectsAverage = 0;
        }
        System.out.print("\nDo you know your quiz average?");
        if (input.equalsIgnoreCase("yes")) {
        System.out.print("\nAverage quiz grade: ");
            QuizzesAverage = grade.nextDouble();
        }
        else{ 
            QuizzesAverage = 0;  
        } 
        System.out.print("\nDo you know your attendance average?");
        if (input.equalsIgnoreCase("yes")){ 
        System.out.print("\nAverage Attendance Grade: ");
            AttendanceAverage = grade.nextDouble();
        }
        else{
            AttendanceAverage = 0;      
        }

        // The user has finished answering the questions. Now the program will automatically calculate the data based on 
        // what the user typed into the program. 

        double CurrentGrade, avgToFinalLetterGrade, WeightandGrade, finalOverallGrade; 

        // The doubles above need to be declared in order for the equations below to work properly. 

        WeightandGrade = (int)((Exam1 * Exam1Grade) + (Exam2 * Exam2Grade) + (finalExam * finalExamGrade) + (Labs * LabAverage) + (Projects * ProjectsAverage) + (Quizzes * QuizzesAverage) + (Attendance * AttendanceAverage));

        CurrentGrade = (int)((WeightandGrade) / (Exam1 + Exam2 + finalExam + Labs + Projects + Quizzes + Attendance ));

        knownWeight = (Exam1 + Exam2 + finalExam + Labs + Projects + Quizzes + Attendance);

        if (grade.equals(A)){ 
            finalOverallGrade = 90;
        }
        else if (grade.equals(B)){ 
            finalOverallGrade = 80;
        }
        else if (grade.equals(C)){ 
            finalOverallGrade = 70;
        }
        else if (grade.equals(D)){ 
            finalOverallGrade = 60;
        }
        else 
            finalOverallGrade = F; 

        avgToFinalLetterGrade = (((100-finalOverallGrade) * (WeightandGrade)) / (100 - knownWeight));

        // The equations above are one of the last parts of the program. These equations are critical to determine whether or not the user received its desired letter grade. 
        // If the desired grade was not reached, the program will give a score that the user must consistently receive in order to possibly reach the desired letter grade.

        if (finalOverallGrade >= 90){ 
            System.out.print("Congratulations! You got an A in the class! Hooray!");
        }
        else if (finalOverallGrade >=80 && finalOverallGrade < 90){ 
            System.out.print("Good job. You got a B in the class!");
        }
        else if (finalOverallGrade >=70 && finalOverallGrade < 80){ 
            System.out.print("You got a C in the class.");
        }
        else if (finalOverallGrade >=60 && finalOverallGrade < 70){
            System.out.print("You got a D in the class.");
        }
        else 
            System.out.print("I'm sorry, but you have a failing grade in the class. May your GPA have mercy on your soul.");
        }

}

2 个答案:

答案 0 :(得分:2)

这段代码有很多问题。 执行double A=90-100;会将A设为-10;

但是,对于您当前的问题:

你做String input = grade.nextLine(); 你永远不会改变输入,因此如果输入不是&#34;是&#34;,它将跳过获得每个作品的等级。

(您可能还想咨询Using scanner.nextLine()使用scan.nextLine()与scanner.nextInt或类似的混合[其他陷阱] [总结:如果你做scanner.nextInt,这并不消耗换行符,所以scanner.nextLine()只会得到那个换行符,而不是你可能期望获得的下一行])

答案 1 :(得分:0)

input = grade.nextLine()使用用户的“百分比权重”输入读取该行的剩余部分。因此,除非用户先验知识输入“是”,否则input将为空。

即,您需要在input之前使用用户输入更新if (input.equalsIgnoreCase("yes")) {...