麻烦做一个for循环与一个do-while循环

问题描述:

刚刚开始使用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(); 



     } 
    } 
} 

尝试这样:

// 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

  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