简体   繁体   English

如何读取两个数字之间的数字?

[英]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. 我对Java相当陌生,我正在尝试编写一个小程序,要求用户输入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. 现在,我希望该数字为1,2或3,否则它将一直循环播放直到用户输入其中一个,但程序此时接受任何数字。 Where am I wrong? 我哪里错了?

You are missing a do keyword in your loop. 您在循环中缺少do关键字。 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. 您的while条件不正确。
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. 要检查,如果char比0大于4 更低的,如果是,它会再次进行循环,当你以后有什么是oposite。
Change the statement to check if myChar is smaller than 1 OR higher than 3. 更改语句来检查,如果myChar大于1小于3 更高。
myChar < 1 || myChar > 3

You are also missing a do at the beginning of the do-while . do-while的开头,您还缺少一个do

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 {...}. 您以错误的方式放置了while,您应该在{...}之前放置一个do-while语句。
  • 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. 您还希望在输入错误的数字(<1或> 3)时运行循环,而不是在输入正确的数字(1至3之间)时运行循环。因此,您还需要更改表达式。

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; 那是你唯一的break; out of it if the scanned value is either 1, 2, or 3. 如果扫描的值是1、2或3,则超出此范围。


@Ali, while(true) approach is perfectly fine. @ Ali, while(true)方法非常好。 In fact, it's far more common to see them than a do-while() in actual code running out there. 实际上,在实际代码中看到它们比在do-while()中见得多。 The downvote received is subjective and based on individual coding style preference rather than an indication on the correctness of the answer. 收到的否决票是主观的,并且基于个人的编码样式偏好,而不是基于答案正确性的指示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM