简体   繁体   中英

Having Trouble Making a for loop with a do-while loop

just started with Java and my assignment this week is to write a program that asks the user how many test scores they want to enter, which the user fills in and then the program asks them to enter test scores up until that counter is met, and display the average amongst the scores.I feel like i have the brunt of it but just need some more help its kind of in a dead loop and I'm stuck. I've moved my for loop around and it either puts my display text into a neverending loop or the program stalls after asking for number of scores to be entered (this is where it is now). Any help is appreciated:

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();



         }  
    }
}

Try something like this:

// 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);
}

I think this should run, but it may have a bug or two. I do not have Java any more, but I will be able to help if it produces an error. Just place this in your class and see how it works. You shouldn't need to modify it at all.

  1. it is not a good idea to have so many nested whiles, it is very inefficient.
  2. it is a good idea to surround the code with try/catch blocks, I didn't put it in there since I'm not sure you have learnt it.
  3. I think you should take user input as total amount of scores, instead of using 100 in the for loop. anyway, this is what I would do, hope this code helps:

     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); } 

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM