1

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);
    }
}
Juan Carlos Mendoza
  • 5,203
  • 7
  • 21
  • 48
  • 2
    I really don't see why you need 2 loops – dumbPotato21 Oct 20 '17 at 18:04
  • 1
    Also, some of your comparisons are incorrect – theGleep Oct 20 '17 at 18:05
  • @FrankUnderwood Inner loop probably handles cases when provide data like `foo bar 123`. It will handle `foo` and `bar` and then accept `123` as valid number. Code most likely based on: [Validating input using java.util.Scanner](https://stackoverflow.com/q/3059333) – Pshemo Oct 20 '17 at 18:06
  • @Luis You posted your code, but didn't describe problem with it. Use [edit] option to clarify your question. – Pshemo Oct 20 '17 at 18:08
  • @Pshemo possibly. There doesn't seem to be a proper description. The code can thus be interpreted in various ways. – dumbPotato21 Oct 20 '17 at 18:08

2 Answers2

1

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);
        }
    }
Maged Saeed
  • 1,574
  • 2
  • 14
  • 32
0

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

Juan Carlos Mendoza
  • 5,203
  • 7
  • 21
  • 48