2013-04-30 116 views
-1

我正在嘗試編寫一個腳本,其中包含可能的迭代次數和爲每個提交的賦值次數獲得的積分。如何在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 
+1

然後解決你的數學。你有什麼問題?如果除了一小部分之外的所有部分都能正常工作,爲什麼還要包含一百行隱藏實際問題的噪聲代碼? – 2013-04-30 04:49:47

+0

檢查你的循環,是嗎? – 2013-04-30 04:51:34

+0

請勿粘貼整個代碼。沒有人會通過它。最好只粘貼代碼有問題的部分或需要幫助的地方 – 2013-04-30 04:52:39

回答

0

這是你的MATH據說出錯了。

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

更改上面的代碼段將: -

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

第一區塊的復位totalEarned & totalWorth值每次迭代,而你需要聚合你的情況。

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";