2014-01-25 185 views
0

剛剛開始使用Java,本週我的任務是編寫一個程序,詢問用戶他們要輸入多少測試分數,然後用戶填入程序要求他們輸入測試分數,直到滿足計數器爲止,並顯示分數中的平均值。我覺得我首當其衝,但只是需要一些更多的幫助,在死循環中這樣做,我被卡住了。我已經移動了我的for循環,它要麼將我的顯示文本置於一個永無止境的循環中,要麼在要求輸入分數(這是它現在的位置)後程序停止。任何幫助表示讚賞:麻煩做一個for循環與一個do-while循環

import java.util.Scanner; 
public class TestScoreApp 
{ 
public static void main(String[] args) 
{ 
    // display operational messages 
    System.out.println("Please enter test scores that range from 0 to 100."); 
    System.out.println(); // print a blank line 

    // initialize variables and create a Scanner object 
    int scoreTotal = 0; 
    int scoreCount = 0; 
    int testScore = 0; 
    int count = 0; 
    Scanner sc = new Scanner(System.in); 
    String choice = "y"; 
    while (!choice.equalsIgnoreCase("n")) 

    while (testScore <= 100) 
    { 

     // get the input from the user 
     System.out.print("Enter the number of scores to be entered: "); 
     for (scoreCount = 0; count <=100; scoreCount++) 
     scoreCount = sc.nextInt();  

     // get the input from the user 
     System.out.print("Enter score: "); 
     testScore = sc.nextInt(); 

     // accumulate score count and score total 
     if (testScore <= 100) 

     { 
      scoreCount = scoreCount + 1; 
      scoreTotal = scoreTotal + testScore; 
     } 
    else if (testScore != 999) 
     System.out.println("Invalid entry, not counted"); 

    // display the score count, score total, and average score 
    double averageScore = scoreTotal/scoreCount; 
    String message = "\n" + 
         "Score count: " + scoreCount + "\n" 
        + "Score total: " + scoreTotal + "\n" 
        + "Average score: " + averageScore + "\n"; 

    System.out.println(message);  
    System.out.println("Enter more test scores? ('y' to continue, 'n' to close): "); 
    choice = sc.next(); 
    System.out.println(); 



     } 
    } 
} 

回答

1

嘗試這樣:

// put this above main() - this way you can use it without ever defining a `Scanner` object again 
static Scanner in = new Scanner(System.in); 

public static void main(String[] args) { 

    // store number of scores to enter 
    int numScores = 0; 

    // input number of scores to enter 
    System.out.print("How many scores would you like to enter? "); 
    numScores = in.nextInt(); 

    // store sum of scores 
    int scoreTotal = 0; 

    // input scores 
    for(int i = 0; i < numScores; i++) 
    scoreTotal += in.nextInt(); 

    // print count, sum, and average of scores 
    System.out.printf("\nScore count: %d", numScores); 
    System.out.printf("\nScore total: %d", scoreTotal); 
    System.out.printf(\n Average score: %d", scoreTotal/numScores); 
} 

我想這應該運行,但它可能有一個或兩個錯誤。我沒有Java了,但如果它產生錯誤,我將能夠提供幫助。只要把它放在你的課堂上,看看它是如何工作的。你根本不需要修改它。

+0

會在哪裏我插入?我把你在我的老式掃描儀下建議的掃描儀對象objhect,把變量與其他變量,並重寫我的輸入語句和程序仍然停止後,用戶輸入分數。 – user3236418

+0

讓我更新我的答案。 – Hosch250

+0

@ user3236418更新是否正常工作? – Hosch250

0
  1. 有這麼多的嵌套whiles是不是一個好主意,它是非常低效的。
  2. 用try/catch塊包圍代碼是一個好主意,我沒有把它放在那裏,因爲我不知道你已經學會了。
  3. 我認爲你應該把用戶輸入作爲分數的總量,而不是在for循環中使用100。 無論如何,這是我會做什麼,希望這有助於代碼:

    import java.util.Scanner; 
    public class test{ 
    public static void main(String[] args){ 
        int count = 0;//amount of scores entered by user 
        int total = 0;//total score added up 
        int current = 0;//current amount of scores entered 
        System.out.println("How many scores would you like to enter?"); 
        Scanner sc = new Scanner(System.in); 
        count = sc.nextInt(); 
        do{ 
         System.out.println("current amount of scores entered is "+current+" please enter the next score"); 
         total += sc.nextInt(); 
         current ++; 
        } 
        while(current < count); 
        System.out.println("the average score of "+current+" entered scores is "+total/count); 
        } 
    

    }

+0

不幸的是,我們應該使用很多嵌套循環,它是我們書中的任務。但是這肯定會幫助我實現這個目標! – user3236418