Java猜谜游戏。如何使用数据验证来检查数字是否在特定范围内?

标签 java

我需要帮助编写一组数据验证语句,检查用户输入是否在 0 到 100 的范围内,并且用户输入的任何内容如果不是 1 到 100 之间的非十进制整数,都应该显示错误消息。另外,我需要一种方法来编码如何获得“再见”输出,以便仅在用户输入“n”而不是“n”和“y”时显示。 N 表示否,y 表示是。

这是我的代码。

import java.util.Scanner;

public class GuessingGameCalc {

    private static void displayWelcomeMessage(int max) {

        System.out.println("Welome to the Java Guessing Game!");
        System.out.println(" ");
        System.out.println("I'm thinking of a number between 1 and" + " " + max + " " + "let's see if you guess what it is!");
        System.out.println(" ");

    }  

    public static int calculateRandomValue(int max) {

      double value = (int) (Math.random() * max + 1);
      int number = (int) value;
      number++;
      return number;
    }

    public static void validateTheData(int count) {
        if( count < 3) {
            System.out.println("Good job!");
        } else if (count < 7) {
            System.out.println("Need more practice.");
        } else{
            System.out.println("Need way more practice.");
        }
    }

    public static void main(String[] args) {
        final int max = 100;
        String prompt = "y";
        displayWelcomeMessage(max);
        int unit = calculateRandomValue(max);
        Scanner sc = new Scanner(System.in);
        int counter = 1;

        while (prompt.equalsIgnoreCase("y")) {
            while (true) {
                System.out.println("Please enter a number.");

                int userEntry = sc.nextInt();

                if (userEntry < 1 || userEntry > max) {
                    System.out.println("Invalid guess! Guess again!");
                    continue;
                }

                if (userEntry < unit) {
                    if ( (unit - userEntry) > 10 ) {
                        System.out.println("Way Too low! Guess higher!");
                    } else {
                        System.out.println("Too low! Guess higher!");
                    }
                } else if (userEntry > unit) {
                    if( (userEntry - unit) > 10 ){
                        System.out.println("Way Too high! Guess lower!");
                    } else {
                        System.out.println("Too high! Guess lower!");
                    }
                } else {
                    System.out.println("Congratulations! You guessed it in" + " " + counter + " " + "tries!\n");
                    validateTheData(counter);
                    break;
                }
                counter++;
            }
            System.out.println("Would you like to try again? Yes or No?");
            prompt = sc.next();
            System.out.println("Goodbye!");
        }
    }
}

最佳答案

不要使用.nextInt(),而是使用.nextLine(),它返回一个String,然后将其解析为 >int 并捕获 NumberFormatException

所以基本上你会有这样的结构:

try {
    int userEntry = Integer.parseInt(sc.nextLine());
    ...
} catch (NumberFormatException nfe) {
    System.out.println("Please enter a valid number.");
}

哦,只是对其余代码的评论。您实际上并不需要两个 while 循环,一个就足够了。

关于Java猜谜游戏。如何使用数据验证来检查数字是否在特定范围内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58161237/

相关文章:

java - 如何将泛型类型绑定(bind)到在层次结构中几乎完全分离的 2 种类型

java - 在 Java 中使用反射时如何避免父对象方法

java - ULP计算浮点

java - 如何在 Java 中从单词 (sWord) 中删除设定的字符串?

java - 什么是供应商在谈论 java 时?

java - 在测试中模拟 CompletionException

java - 构造函数还是main()?如何将参数传递给方法?

java - Hibernate 不会完全刷新实体子项

java - 委托(delegate)对象产生重复代码 : (inheritance vs delegates)

java - 使用工作线程时返回语句