简体   繁体   中英

how to do keep asking the user to enter the right number

I need to use a while loop to ask the user for a number that is between 1-100, and tell the user that they entered the wrong number if they enter any number that is negative or over 100. Here is what I have so far. Whenever I run it, it asks for the user's input. When the input is negative or above 100 it says invalid number, yet when the user input is 45, it still says invalid number, when a number between 0-100 is valid. I don't think its reading the last part of the code.

import java.util.*;

public class PlayOffs {
    public static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.print("What is the % chance Team 1 will win (1-99)?: ");
        int team1input = scan.nextInt();
        do {
            while (team1input >= -1 || team1input >= 101) {
                System.out.println("That's not between 1-99");
                scan.next(); // this is important!
            }
            team1input = scan.nextInt();
        } while (team1input >= 0 && team1input <= 100);
        System.out.println("Thank you! Got " + team1input);
    }
}

You have problems with your comparisons.
You do not need two loops.
This code might be suitable.

import java.util.Random;
import java.util.*;

    public class PlayOffs {
        public static Scanner scan = new Scanner(System.in);

        public static void main(String[] args) {

            System.out.print("What is the % chance Team 1 will win (1-99)?: ");
            int team1input = scan.nextInt();
            do {
                if(!(team1input >= 0 && team1input <= 100)) {
                    System.out.println("That's not between 1-99");
                    scan.next(); // this is important!
                    team1input = scan.nextInt();
                }

            } while (!(team1input > -1 && team1input  <101));
            System.out.println("Thank you! Got " + team1input);
        }
    }

Your problem is in this condition of your while loop:

while (team1input >= -1 || team1input >= 101)

45 >= -1? => true, and that is why it prints that is invalid.

Actually you don't need 2 loops for this. The do-while loop can be enough to get your desired result:

import java.util.*;

public class PlayOffs {
    public static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.print("What is the % chance Team 1 will win (0-100)?: ");
        boolean validNumber;
        int team1input;
        do {
            team1input = scan.nextInt();
            validNumber = team1input >= 0 && team1input <= 100;

            if (!validNumber) {
                System.out.println("That's not between 0-100");
            }

        } while (!validNumber);
        System.out.println("Thank you! Got " + team1input);
    }
}

Run output:

What is the % chance Team 1 will win (0-100)?: -1
That's not between 0-100
105
That's not between 0-100
101
That's not between 0-100
45
Thank you! Got 45

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