简体   繁体   中英

Taking scanner input and outputting min and max

so I'm trying to find the min, max, and average number of the numbers input. Everything works fine, although I do not want input less than 0 or greater than 100. When I input a number less than 0 or greater than 100 it still records it as the min/max. I do not want this! How would I not take input that is less than 0 or greater than 100? Thanks!

import java.text.DecimalFormat;
import java.util.Scanner;
public class ExamGrades {

public static void main(String[]args){

    Scanner scan = new Scanner(System.in );

        int number = 0;
        double total = 0;
        int minimum;
        int maximum = 0;

        System.out.println("Please enter the first integer: ");

        number = scan.nextInt();
        minimum=number;
        total += number;

        for(int i = 2; i<11; i++){

            if(number<0 || number >100){
                System.out.println("Please enter a valid number: ");
                number = scan.nextInt();
                i--;
            }
            else{
                System.out.println("Please enter integer " + i + ":");
                number = scan.nextInt();
                total += number;

            if(number<minimum)
                minimum = number;
            if(number>maximum)
                maximum = number;
            }
        }

        DecimalFormat oneDecimalPlace = new DecimalFormat("##.#");
        System.out.println("The minimum is: " + minimum);
        System.out.println("The maximum is: " + maximum);
        System.out.println("The average is: " + oneDecimalPlace.format((total) / 10.0 ));

    }
}

problem:

if(number<0 || number >100)

It will return false when number is 100 or 0 thus accepting it and executing you else block.

Is 100>100 answer false because they are equal, not greater than the other

solution:

if(number<1 || number >99)

EDit:

public static void main(String[]args){

    Scanner scan = new Scanner(System.in );

        int number = 0;
        double total = 0;
        int minimum = 0;
        int maximum = 0;

        System.out.println("Please enter the first integer: ");

        number = scan.nextInt();
        if(number>1 && number <99)
        {
        minimum=number;
        total += number;
        }
        for(int i = 2; i<11; i++){

            if(number<1 || number >99){
                System.out.println("Please enter a valid number: ");
                number = scan.nextInt();
                i--;
            }
            else{
                System.out.println("Please enter integer " + i + ":");
                number = scan.nextInt();
                total += number;

                if(number>1 && number <99)
                {
                     if(number<minimum)
                         minimum = number;
                     if(number>maximum)
                         maximum = number;
                }

            }
        }

        DecimalFormat oneDecimalPlace = new DecimalFormat("##.#");
        System.out.println("The minimum is: " + minimum);
        System.out.println("The maximum is: " + maximum);
        System.out.println("The average is: " + oneDecimalPlace.format((total) / 10.0 ));


}

Let's say you entered 5 for integer one, then 102 for the second integer. What's going to happen? Well before you entered 102 , number was 5 , so it will go into the else block.

It'll say:

Please enter integer 2:

then you type: 102

So then what? Well the next piece of code is:

number = scan.nextInt(); and then it goes through the if-statements right below it to determine if it's a maximum. Nothing is stopping it.

Try this code instead of your loop:

System.out.println("Please enter the first integer: ");

number = scan.nextInt();

// keep them in a loop until they enter a valid number
while (number < 0 || number > 100) {
    System.out.println("Please enter a valid number: ");
    number = scan.nextInt();
}

// after they entered a valid number, add it to the series
minimum = number;
total += number;

for(int i = 2; i < 11; i++) {

    System.out.println("Please enter integer " + i + ":");
    number = scan.nextInt();

    // keep them in a loop until they enter a valid number
    while (number < 0 || number > 100) {
        System.out.println("Please enter a valid number: ");
        number = scan.nextInt();
    }

    // after they entered a valid number, add it to the series

    total += number;

    if(number<minimum)
        minimum = number;
    if(number>maximum)
        maximum = number;
}

Here is an Example.Exceptions are not handled so put a Try-catch to String to Integer Conversion If you want.

import java.util.Scanner;
import java.text.DecimalFormat;

public class Numbers{
    public static void main(String x[]){
        Scanner scn=new Scanner(System.in);
        int Total=0,Max=0,Min=Integer.MAX_VALUE;
        for(int i=0;i<10;){
            System.out.print("Enter Number :");         
            int NumberOne=Integer.parseInt(scn.nextLine());//Put a Try catch If needed
            if(NumberOne>0&&NumberOne<100){
                Total+=NumberOne;
                Max=(Max>NumberOne)?Max:NumberOne;
                Min=(Min<NumberOne)?Min:NumberOne;
                i++;
            }else{
                System.out.print("Number Invalid");         
            }
        }
        DecimalFormat oneDecimalPlace = new DecimalFormat("##.#");
        System.out.println("The minimum is: " + Min);
        System.out.println("The maximum is: " + Max);
        System.out.println("The average is: " + oneDecimalPlace.format((Total) / 10.0 ));
    }

}

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