2013-11-28 24 views
0

我正在寫一個java程序,它將從鍵盤4個輸入讀取,名爲Test1,Test2,Test3,Final。然後,程序將確定測試1,測試2和測試3的3個測試等級中的最佳2(BTest1,BTest2)。然後,它將使用以下評分策略計算最終等級: BTest1:30%BTest2:30最終%:40%刪除最低級的java程序

這裏是我的了:

public static void main (String[] args) 
    { 

     Scanner scan = new Scanner (System.in); 


     int Test1 = 0, 
       Test2 = 0, 
       Test3 = 0, 
       Final = 0; 


     double AVG; 

     // Test 1 input 
     System.out.println ("Enter Test 1: "); 
     Test1 = scan.nextInt(); 

     while ((Test1 < 0) || (Test1 > 100)) 
     { 
      System.out.println ("Invalid Input, try again."); 
      Test1 = scan.nextInt(); 

     } 

     // Test 2 input 
     System.out.println ("Enter Test 2: "); 
     Test2 = scan.nextInt(); 

     while ((Test2 < 0) || (Test2 > 100)) 
     { 
      System.out.println ("Invalid Input, try again."); 
      Test2 = scan.nextInt(); 

     } 

     // Test 3 input 
     System.out.println ("Enter Test 3: "); 
     Test3 = scan.nextInt(); 

     while ((Test3 < 0) || (Test3 > 100)) 
     { 
      System.out.println ("Invalid Input, try again."); 
      Test3 = scan.nextInt(); 

     } 

     // Final Exam input 
     System.out.println ("Enter Final Exam: "); 
     Final = scan.nextInt(); 

     while ((Final < 0) || (Final > 100)) 
     { 
      System.out.println ("Invalid Input, try again."); 
      Final = scan.nextInt(); 

     } 


     // Find the highest out of the 3 tests 
     int BTest1, 
      BTest2; 

     if ((Test1 >= Test2) && (Test1 >= Test3)) 
      BTest1 = Test1; 
     if ((Test2 >= Test1) && (Test2 >=Test3)) 
      BTest2 = Test2; 
     if ((Test3 >= Test1) && (Test3 >= Test2)) 
      BTest2 = Test3; 


     // Compute the Average 
     AVG = ((BTest1 * .3) + (BTest2 * .3) + (Final * .4)); 

       if (AVG >= 90) 
        System.out.println ("A " + AVG); 
       else 
        if (AVG >= 80) 
         System.out.println ("B " + AVG); 
        else 
         if (AVG >= 70) 
          System.out.println ("C " + AVG); 
         else 
          if (AVG >= 60) 
           System.out.println ("D " + AVG); 
          else 
           System.out.println ("F " + AVG); 



    } 
} 

,我遇到的問題是,我不能放棄最低等級。 有人可以請指導我正確的方向嗎?

預先感謝您!

回答

0

您的問題是,您認爲Test1會自動高於Test2或Test3,但如果不是(例如10,50,60),BTest1將永遠不會設置。爲了做到這一點,你需要很長的一系列if/else調用。但有一個簡單的方法:

public static void main(String[] args) { 
    final Scanner scan = new Scanner(System.in); 

    final int[] testScores = new int[3]; // use an array here 
    Arrays.fill(testScores, -1); // set all to -1 to know if we already got a proper input 

    for (int i = 0; i < testScores.length; ++i) { // for loop simplifies the whole thing a lot 
    while ((testScores[i] < 0) || (testScores[i] > 100)) { 
     System.out.println("Please enter Test " + (i + 1) + ":"); 
     testScores[i] = scan.nextInt(); 
    } 
    } 

    int exam = -1; // Final is a bad name, because final is a reserved word 
    while ((exam < 0) || (exam > 100)) { 
    System.out.println("Please enter Final Exam:"); 
    exam = scan.nextInt(); 
    } 

    Arrays.sort(testScores); // automatically sorts the entry from lowest to highest 
    final double AVG = (testScores[testScores.length - 1] + testScores[testScores.length - 2]) * 0.3 + exam * 0.4; // therefore the best grades are the last two entries 

    if (AVG >= 90) { 
    System.out.println("A " + AVG); 
    } else if (AVG >= 80) { 
    System.out.println("B " + AVG); 
    } else if (AVG >= 70) { 
    System.out.println("C " + AVG); 
    } else if (AVG >= 60) { 
    System.out.println("D " + AVG); 
    } else { 
    System.out.println("F " + AVG); 
    } 
} 
-1

你可以這樣做:

TreeSet<Integer> tests = new TreeSet<Integer>(); 
tests.add(test1); 
tests.add(test2); 
tests.add(test3); 

if (tests.size() == 1) { 
    bTest1 = tests.last(); 
    bTest2 = tests.poolLast(); 
} else { 
    bTest1 = tests.poolLast(); 
    bTest2 = tests.poolLast(); 
} 

它會使至多3個元素的組,然後檢索設定的2個大要素(也處理的情況時,只有1組元素 - 所有插入的值都是相同的)。

看看Java naming conventions。你所有的變量看起來都像我的類。

編輯:更改bTest1bTest2的地方 - 不是它會改變任何東西(請參閱評論)。

+0

此外,該功能被命名爲'pollLast'您的代碼將賣到最好的等級爲bTest2(這是第二個最好的),且第二最好到bTest1。只有1個元素的檢查會產生不存在的假等級。 – TwoThe

+0

正如我所看到的,它需要輸入三個測試結果的值,並將這些值顯式設置爲「測試」。 'test.size()== 1'只適用於'test1 == test2 == test3'的情況。所以它不會產生任何假等級。 事情bTest *先*而哪個*最後*可以很容易地修復 - 而不是它會改變任何事情,因爲在最終成績計算中沒有假設它們的任何順序。 –