简体   繁体   中英

|| in while loop not working as expected

I'm having an issue that I cannot seem to fix. My goal is to prompt the user for the number of people playing the game. I want to continue prompting the user while their input meets the following requirements: -input is not an integer -input is less than three -input is larger than seven

What is happening is that it seems to require 3 inputs to check the third condition, one for the first and two for the second. It is not checking them all at once, and I assume it has something to do with the syntax .nextInt(), but I cannot find another way to express this. Thank you ahead of time for your help!

Here is the code:

public void setNumPlayers(Game game) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");
    while(!input.hasNextInt() || input.nextInt() < 3 || input.nextInt() > 7) {
        System.out.println("Please eneter the number of people playing. You must have at least three players, and no more than seven.");
        input.next();
    }
    game.setNumPlayers(input.nextInt());
    input.close();
}

and the call in the main

Game g = new Game();
    ConsoleOutput io = new ConsoleOutput();
    io.setNumPlayers(g);
    System.out.println(g.getNumPlayers());

EDIT: I've changed the code to store the nextInt as a variable. The following code works, prompts me if I enter letters, prompts me and lets me reassign x if I enter a number outside of its parameters, the only issue is that If I enter an incorrect number, THEN a letter, it crashes...should I encapsulate this in some kind of a try/catch? That doesnt seem practical.

public void setNumPlayers(Game game) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");     

    while(!input.hasNextInt()) {            
        System.out.println("Please eneter the number of people playing.");
        input.next();
    }
    int x = input.nextInt();
    while(x<3 || x>7) {
        System.out.println("The number of players must be at least three and no more than seven.");
        x = input.nextInt();              
    }

    game.setNumPlayers(x);
    input.close();
}

By calling Scanner.nextInt() twice, you read two numbers, not the same one twice.

Consider reading the int value, store it in a variable and use that in your if statement.

You have to modify a little bit the logic, if you want to check input and continue to ask to enter until the input is good. The following will work for a sequence of inputs like:

How many people are playing?
Please enter the number of people playing. You must have at least three players, and no more than seven.
10
Please enter the number of people playing. You must have at least three players, and no more than seven.
toto
Please enter the number of people playing. You must have at least three players, and no more than seven.
tutu
Please enter the number of people playing. You must have at least three players, and no more than seven.
22
Please enter the number of people playing. You must have at least three players, and no more than seven.
6


    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");

    int numPlayers = 0;
    while (numPlayers < 3 || numPlayers > 7) {
        System.out.println("Please enter the number of people playing. You must have at least three players, and no more than seven.");
        if (!input.hasNextInt()) {
            input.next();
        }
        else {
            numPlayers = input.nextInt();
        }
    }
    game.setNumPlayers(numPlayers);
    input.close();

use && operator. and also use a local variable and assign the value from scanner. and use it in condition.

int x =0;
 if(input.hasNextInt())
 {
 x= input.nextInt();
 }
 while( x > 3 && x < 7) {
    System.out.println("Please eneter the number of people playing. You must have at least three players, and no more than seven.");
   input.next();
 }

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