-1

我正在尝试编写一个脚本,该脚本具有可能的积分迭代以及为每个提交的作业数量获得的积分。

然后,我想为可能的总积分和获得的总积分创建一个变量,以便能够创建一个显示这些数字的进度报告。

这是代码,一切正常,除了我的数学在可能的总数和总收入上是错误的。

String anotherCourse = "yes"; // variable to control running program
                                    // again
    do {
        String studentName = ""; // Student Name
        String courseName = ""; // Course Name
        int assignSub = 0; // Number of Assignments submitted
        double pointsWorth = 0; // Points worth for each assignment
        double pointsEarned = 0; // Points earned for each assignment

        // Calculated

        double currentPercent = 0; // Current Percentage
        String letterGrade = ""; // Letter Grade

        // Display header
        System.out.println("\t\t---------------------------");
        System.out.println("\t\t   What's Your Grade???");
        System.out.println("\t\t---------------------------");

        // Ask for student name and course name
        System.out.print("Enter student name: ");
        studentName = input.nextLine();
        System.out.print("Enter course name: ");
        courseName = input.nextLine();
        System.out.println();

        // Ask how many assignments were taken
        System.out.print("How many assignments have you submitted: ");
        assignSub = input.nextInt();

        while (assignSub < 0) {
            System.out
                    .println("Assignments submitted point value must be greater than 0...Try Again!");
            System.out.println();
            System.out.print("How many assignments have you submitted: ");
            assignSub = input.nextInt();
        }
        // and error code if is <= to 0 they take in again

        int totalEarned = 0;
        int totalWorth = 0;
        for (int i = 1; i <= assignSub; i++) {

            // How many points is the assignment worth?
            System.out.println();
            System.out.print("How many points was assignment " + i
                    + " worth: ");
            pointsWorth = input.nextDouble();

            while (pointsWorth < 0) { // Error Message
                System.out
                        .println("Assignment point value must be greater than 0...Try Again!");
                System.out.println();
                System.out.print("How many points was assignment " + i
                        + " worth: ");
                pointsWorth = input.nextDouble();
            } // end while

            System.out.print("How many points did you score: ");
            pointsEarned = input.nextDouble();
            if (pointsEarned >0 && pointsEarned < pointsWorth){

            }
            else  { //Error Message
                System.out.println("Points scored must be between assignment worth and greater than or equal to 0...Try Again!");
                System.out.println();
                System.out.print("How many points did you score: ");
                pointsEarned = input.nextDouble();
            }// end while

            totalEarned = (int) (pointsEarned + i);
            totalWorth = (int) (pointsWorth + i);



        }// end for

        System.out.println();
        System.out.println("\tProgress Report for " + studentName);
        System.out.println("\tCourse Name is " + courseName);
        System.out
                .println("--------------------------------------------------");

        System.out
                .println("Number of assignments submitted....." + assignSub);
        System.out.println("Total points possible..............."
                + totalWorth);
        System.out.println("Total points earned................."
                + totalEarned);

        // calculate the current percentage

        currentPercent = (totalEarned / totalWorth) * 100;
        System.out.println("Total percent to date...............%.2f%%\n"
                + currentPercent);

        // Write a nested if else statement to figure out letter grade based
        // on standard grade chart

        if (currentPercent <= 100 && currentPercent >= 93)
            letterGrade = "A";
        else if (currentPercent < 93 && currentPercent >= 90)
            letterGrade = "A-";
        else if (currentPercent < 90 && currentPercent >= 87)
            letterGrade = "B+";
        else if (currentPercent < 87 && currentPercent >= 83)
            letterGrade = "B";
        else if (currentPercent < 83 && currentPercent >= 80)
            letterGrade = "B-";
        else if (currentPercent < 80 && currentPercent >= 77)
            letterGrade = "C+";
        else if (currentPercent < 77 && currentPercent >= 73)
            letterGrade = "C";
        else if (currentPercent < 73 && currentPercent >= 70)
            letterGrade = "C-";
        else if (currentPercent < 70 && currentPercent >= 67)
            letterGrade = "D+";
        else if (currentPercent < 67 && currentPercent >= 63)
            letterGrade = "D";
        else if (currentPercent < 63 && currentPercent >= 60)
            letterGrade = "D-";
        else
            letterGrade = "E";

        System.out.println("Letter grade to date.................."
                + letterGrade);

        System.out.println();
        System.out
                .print("--------------------------------------------------");



        // This code ends the do while to run entire program again
        System.out
                .println("Enter yes if there is another class you want to calculate: ");
        anotherCourse = input.next();
        input.nextLine(); // causes skipping issue to fix
        System.out.print("\n\n\n");
    } while (anotherCourse.equalsIgnoreCase("yes"));
    System.out
            .print("-----------------------------------------------------");
} // end of main
4

2 回答 2

0

这就是你MATH应该出错的地方。

totalEarned = (int) (pointsEarned + i);
totalWorth = (int) (pointsWorth + i);

将上面的代码片段更改为:-

totalEarned = totalEarned + (int)pointsEarned;
totalWorth = totalWorth + (int)pointsWorth;

第一个块是为每次迭代重置totalEarned&的值totalWorth,而你需要在你的情况下聚合它。

于 2013-04-30T04:52:34.737 回答
0

此外,无需在 else-if 循环中再次检查上限。

   if (currentPercent >= 93)
        letterGrade = "A";
    else if (currentPercent >= 90)
        letterGrade = "A-";
    else if (currentPercent >= 87)
        letterGrade = "B+";
    else if (currentPercent >= 83)
        letterGrade = "B";
    else if (currentPercent >= 80)
        letterGrade = "B-";
    else if (currentPercent >= 77)
        letterGrade = "C+";
    else if (currentPercent >= 73)
        letterGrade = "C";
    else if (currentPercent >= 70)
        letterGrade = "C-";
    else if (currentPercent >= 67)
        letterGrade = "D+";
    else if (currentPercent >= 63)
        letterGrade = "D";
    else if (currentPercent >= 60)
        letterGrade = "D-";
    else
        letterGrade = "E";
于 2013-04-30T04:58:05.617 回答