简体   繁体   中英

How can I read a number in between 2 numbers?

I am fairly new to Java and I am trying to write a small program that asks a user to enter an integer between 0-4. I have written this so far and but it doesn't seem to work! Can anyone tell me where am I wrong?

import java.util.Scanner;
public class GameCharSelect {

    public static void main(String[] argh){
        int myChar;
        Scanner in = new Scanner(System.in);
        {
            System.out.print("choose a player: ");
            myChar = in.nextInt();
        }while(myChar>0 && myChar<4);

        System.out.println("--------");
        System.out.println("you chose "+ myChar);

    }

}

Now I want the number to be 1,2 or 3 or else it loop until the user input one of these but the program accept any number at the moment. Where am I wrong?

You are missing a do keyword in your loop. Also your conditional should be reversed:

public static void main(String[] argh) {
    int myChar;
    Scanner in = new Scanner(System.in);
    do {
        System.out.print("choose a player: ");
        myChar = in.nextInt();
    } while (myChar <= 0 || myChar >= 4);

    System.out.println("--------");
    System.out.println("you chose " + myChar);

}

Your while condition is wrong.
You are checking if the char is larger than 0 AND lower than 4, and if it is, it will do the loop again, while what you are after is the oposite.
Change the statement to check if myChar is smaller than 1 OR higher than 3.
myChar < 1 || myChar > 3

You are also missing a do at the beginning of the do-while .

You haf two problems in your code:

  • You are putting the while in a wrong way, you should put a do-while statement or put the while before the {...}.
  • You also want to run the loop when you put a wrong number (<1 or >3), not when you put the correct number(between 1 and 3)... So you also need to change the expression.

My code would be something like this:

import java.util.Scanner;
public class GameCharSelect {

    public static void main(String[] argh){
        int myChar;
        Scanner in = new Scanner(System.in);
        do{
            System.out.print("choose a player: ");
            myChar = in.nextInt();
        } while(myChar<1 || myChar>3);

        System.out.println("--------");
        System.out.println("you chose "+ myChar);

    }

}

Your loop should look like

while (true) {
    System.out.print("Choose a player: ");
    myChar = in.nextInt();
    if (myChar > 0 && myChar < 4) {
        break; // out of the loop
    }
}

That is you only break; out of it if the scanned value is either 1, 2, or 3.


@Ali, while(true) approach is perfectly fine. In fact, it's far more common to see them than a do-while() in actual code running out there. The downvote received is subjective and based on individual coding style preference rather than an indication on the correctness of the answer.

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