Thatoneguy Thatoneguy - 1 month ago 11
Java Question

Java Loops are kicking my butt

Hi i am having trouble with loops. I am confused on how to set up a way to get the


  • the lowest score

  • the highest score

  • the average of the scores



and If no scores were entered, display the message that “No test grade scores were entered”.

i also had to sent up a counter which i did, and i also had to validate if the score was from 0 to 100 which i did i just don't know what to do next

import java.util.Scanner;

public class loops {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

int average = 0;
int count = 0;
int score;



System.out.print("Please enter first score:");
score = keyboard.nextInt();




while (score!=-1){

while ((score>=0)&&(score<=100)){

System.out.println("the score is between 0 to 100 ");
System.out.println("Please enter the next test score:");
score = keyboard.nextInt();
count = count + 1;

}


}


average = (score/count);
System.out.println("The average is " +average);
System.out.println("The number of test scores enter was:"+count);


}

}

Answer

See explanations in comments:

import java.util.Scanner;

public class Loops { //use java naming convention

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int  count = 0,  score = 0, min = 0, max = 0, sum =0;
        float average = 0;//the average might not be int

        System.out.print("Please enter first score:");
        score = keyboard.nextInt();

        //add it to sum
        sum = score;
        //keep first number as min and max
        min = score;  max = score;

        count++;//increment counter

        //this is not needed, score of -1 will stop the next loop any way
        //while (score!=-1){

        while (true){

            System.out.println("the score is between 0 to 100 ");
            System.out.println("Please enter the next test score, or -1 to quit:");
            score = keyboard.nextInt();

            if((score < 0) ||(score > 100)) {
                break;
            }
            count++;//increment counter

            //you need to sum all entered numbers
            sum += score;

            //check if entered number is min
            if(score < min) {
                min = score ;
            }

            //check if entered number is max
            if(score >  max) {
                max = score ;
            }
        }

        if(count >0 ) {
            average = ((float)sum/count);
            System.out.println("The average is " +average );
            System.out.println("The min is " +min);
            System.out.println("The max is " +max);
            System.out.println("The number of test scores enter was:"+count);
        }else {
            System.err.println("No numbers entered");
        }
    }
}

Don't hesitate to ask for clarifications as needed.