【问题标题】:how to do keep asking the user to enter the right number如何继续要求用户输入正确的数字
【发布时间】:2018-04-01 23:05:59
【问题描述】:

我需要使用一个 while 循环来询问用户一个介于 1-100 之间的数字,如果他们输入任何负数或超过 100 的数字,则告诉用户他们输入了错误的数字。这就是我到目前为止。每当我运行它时,它都会要求用户输入。当输入为负数或大于 100 时,表示无效数字,而当用户输入为 45 时,仍然表示无效数字,当 0-100 之间的数字有效时。我不认为它在阅读代码的最后一部分。

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);
    }
}

【问题讨论】:

  • 我真的不明白你为什么需要 2 个循环
  • 另外,你的一些比较是不正确的
  • @FrankUnderwood 内循环可能会处理提供像foo bar 123 这样的数据的情况。它将处理foobar,然后接受123 作为有效数字。代码最可能基于:Validating input using java.util.Scanner
  • @Luis 你发布了你的代码,但没有描述它的问题。使用edit 选项来澄清您的问题。
  • @Pshemo 可能。好像没有合适的描述。因此,可以以多种方式解释代码。

标签: java loops input while-loop


【解决方案1】:

您的比较有问题。
您不需要两个循环。
这段代码可能合适。

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);
        }
    }

【讨论】:

  • 当你输入 45 时会打印出无效的数字。检查 if 语句中的条件。
【解决方案2】:

您的问题在于您的 while 循环:

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

45 >= -1? => true,这就是它打印无效的原因。

实际上,您不需要 2 个循环。 do-while 循环足以获得您想要的结果:

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);
    }
}

运行输出:

第 1 队获胜的几率是多少(0-100)?:-1
那不是 0-100之间
105
那不是0-100之间
101
那是 不在 0-100 之间
45
谢谢!得到 45

【讨论】:

  • 为什么是while(!validNumber)而不是while(validNumber)?
  • 您想在号码无效有效时继续循环。这就是while(!validNumber) 所做的。当你得到一个有效的数字时,你就可以退出循环了。
猜你喜欢
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 2022-11-16
  • 2015-12-09
相关资源
最近更新 更多