如何在Java中添加迭代值

时间:2013-04-30 04:48:13

标签: java

我正在尝试编写一个脚本,其中包含可能的点数迭代以及为每个提交的作业获得的分数。

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

以下是代码,一切正常,只是我的数学错误总数和总收入。

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

2 个答案:

答案 0 :(得分:0)

这是你MATH出错的地方。

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

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

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

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

答案 1 :(得分: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";