0

My program asks a user for test scores and displays the min/max, # of tests, and the average. However, if the score isn't between 0 and 10 I don't want that number used in my calculations. How would I go about doing that in my do/while loop? I've tried entering System.exit(0); after line if (num <= 0 || num >10) System.out.println("Score must be between 10 and 0"); but it breaks my program. Here is my code:

import java.util.*;
public class QuizScoreStatistics {
public static void main (String args[]) {
      float max=0;
    float min=99;
    float avg=0;
    float count=0;
    float sum=0;
    float num;
    Scanner scan=new Scanner(System.in);
    System.out.println("Please input test scores with values between 0-10.\nEnter 99 to finish.");
    num=scan.nextInt();
    float temp=num;

    do
    {
        if(num==99) 
            System.exit(0);

        if (num <= 0 || num >10)
        System.out.println("Score must be between 10 and 0");



        if(num>=0 && num<=100)
        {
            count++;
            sum+=num;
            if(num>max ) 
                max=num;
            if(num<min)
                min=num;
        }
    } while((num=scan.nextInt())!=99);

   // System.out.println("Test Statistics:");
    System.out.println("Number of tests: "+count);
    System.out.println("Lowest: "+min);
    System.out.println("Highest: "+max);;
    System.out.printf("Average: " + sum/count);
}

}

Nitro
  • 87
  • 6
  • After you fix your loop read the following: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – PM 77-1 Apr 04 '20 at 19:09
  • Nitro - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 18 '20 at 17:37

3 Answers3

3

You can try continuing the rest of the do-while loop.

if (num <= 0 || num >10){
        System.out.println("Score must be between 10 and 0");
        continue;
}
Buddhima Gamlath
  • 2,218
  • 12
  • 25
0

Do a continue

if(num <= 0 || num > 10){
    System.out.println("wrong asnwer");
    num = scan.nextInt();
    continue;
}

continue will skip to the next iteration of the loop. So you start the loop over.

Jacqques
  • 31
  • 3
0

You need to address a couple of things:

  1. Do not use num = scan.nextInt() twice. For this, you need to place num = scan.nextInt(); inside do...while loop.
  2. Use continue when the number is not between 0 and 10
  3. Break the loop when number is not 99.

import java.util.Scanner;

public class Main {

    public static void main(String args[]) {
        float max = 0;
        float min = 99;
        float avg = 0;
        float count = 0;
        float sum = 0;
        float num;
        Scanner scan = new Scanner(System.in);
        System.out.println("Please input test scores with values between 0-10.\nEnter 99 to finish.");
        float temp;

        do {
            num = scan.nextInt();
            if ((num <= 0 || num > 10) && num != 99) {
                System.out.println("Score must be between 10 and 0");
                continue;
            }
            temp = num;
            if (num >= 0 && num <= 100) {
                count++;
                sum += num;
                if (num > max)
                    max = num;
                if (num < min)
                    min = num;
            }
        } while (num != 99);

        // System.out.println("Test Statistics:");
        System.out.println("Number of tests: " + count);
        System.out.println("Lowest: " + min);
        System.out.println("Highest: " + max);
        System.out.printf("Average: " + sum / count);
    }
}

A sample run:

Please input test scores with values between 0-10.
Enter 99 to finish.
1
23
Score must be between 10 and 0
2
99
Number of tests: 3.0
Lowest: 1.0
Highest: 99.0
Average: 34.0

Feel free to comment in case of any doubt/issue.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72