需要验证整数;hasNextInt 不起作用。

huangapple go评论63阅读模式
英文:

Need to validate int; hasNextInt isn't working

问题

我正在解决一个创建猜数字游戏的作业问题。我已经解决了那部分,但我们需要验证输入。我尝试使用hasNextInt,但我一直得到一个错误,错误消息说“int 无法被解引用”,并指向了!guess.hasNextInt这行代码。

我尝试了很多次,但仍然得到相同的错误。我包含的代码只是我最近的尝试。

如何让hasNextInt工作,或者我应该如何验证输入?

import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        int num = (int) (Math.random() * 101);

        Scanner input = new Scanner(System.in);
        System.out.println("欢迎来到我的猜数字游戏!");

        int guess = -1;
        // 循环,直到猜中数字
        while (guess != num) {
            System.out.print("猜一个介于1和100之间的数字:");
            guess = input.nextInt();

            // 验证输入
            while (!input.hasNextInt()) {
                System.out.println("无效的输入,请重试。");
                input.next();
            }

            if (guess == num)
                System.out.println("正确!");
            else if (guess < num)
                System.out.println("你猜的数字太低了");
            else
                System.out.println("你猜的数字太高了");

        }
    }
}
英文:

I'm working on a homework problem of creating a guessing game. I've got that part working, but we have to validate the input. I've tried using hasNextInt, but I keep getting an error saying "int cannot be dereferenced" and points to the "!guess.hasNextInt" code.

I've tried many iterations, but I still get the same error. The code I'm including is just my most recent try.

How do I get hasNextInt to work or how else should I validate the input?

import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        int num = (int) (Math.random() * 101);

        Scanner input = new Scanner(System.in);
        System.out.println(&quot;Welcome to my Guessing Game!&quot;);

        int guess = -1;
        //Loop goes as long as guess doesn&#39;t equal num
        while (guess != num) {
            System.out.print(&quot;Guess a number between 1 and 100:   &quot;);
            guess = input.nextInt();

            //Validates input
            while (!guess.hasNextInt()) {
                System.out.println(&quot;Invalid response, try again.&quot;);
                in.next();
            }

            if (guess == num)
                System.out.println(&quot;Correct!&quot;);
            else if (guess &lt; num)
                System.out.println(&quot;Your guess was too low&quot;);
            else
                System.out.println(&quot;Your guess was too high&quot;);

        }
    }
}


</details>


# 答案1
**得分**: 1

我修复了代码:

```java
public static void main(String[] args) {
    int num = (int) (Math.random() * 101);
    System.out.println(num);
    Scanner input = new Scanner(System.in);
    System.out.println("欢迎来到猜数字游戏!");

    int guess = -1;
    // 循环持续到猜测与随机数相等
    while (guess != num) {
        System.out.print("猜一个1到100之间的数字:   ");
        guess = input.nextInt();

        if (guess == num) {
            System.out.println("正确!");
            break;
        } else if (guess < num)
            System.out.println("你猜的数字太小了");
        else
            System.out.println("你猜的数字太大了");

        // 验证输入的合法性
        while (!input.hasNextInt()) {
            System.out.println("无效的输入,请重试。");
            input.next();
        }
    }
}

如果用户猜对了数字,游戏会使用break结束。

while (!guess.hasNextInt())这一行中,你使用了一个整数,但它应该是期望一个Scanner对象input

英文:

i fixed the code:

public static void main(String[] args) {
    int num = (int) (Math.random() * 101);
    System.out.println(num);
    Scanner input = new Scanner(System.in);
    System.out.println(&quot;Welcome to my Guessing Game!&quot;);

    int guess = -1;
    //Loop goes as long as guess doesn&#39;t equal num
    while (guess != num) {
        System.out.print(&quot;Guess a number between 1 and 100:   &quot;);
        guess = input.nextInt();

        if (guess == num) {
            System.out.println(&quot;Correct!&quot;);
        	break;}
        else if (guess &lt; num)
            System.out.println(&quot;Your guess was too low&quot;);
        else
            System.out.println(&quot;Your guess was too high&quot;);

        //Validates input
        while (!input.hasNextInt()) {
            System.out.println(&quot;Invalid response, try again.&quot;);
            input.next();
        }

       
    }
}

if the user guessed the number the game ends using break

in while (!guess.hasNextInt()) you were using a integer where it's expect the Scanner input

答案2

得分: 1

如果您想解析您的数字,您可以使用Integer.parseInt()方法,如下所示。同时,您错误地使用了hasNextInt()方法。您还没有将in.next()的值存储在任何变量中。

import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        int num = (int)(Math.random() * 101);

        Scanner input = new Scanner(System.in);
        System.out.println("欢迎参加猜数字游戏!");

        int guess = -1;
        // 循环直到猜测等于num
        while (true) {
            System.out.print("猜一个1到100之间的数字:   ");
            String numberString = input.nextLine();

            // 验证输入
            try {
                guess = Integer.parseInt(numberString);
                if (guess == num) {
                    System.out.println("正确!");
                    break;
                } else if (guess < num)
                    System.out.println("您猜的数字太小了");
                else
                    System.out.println("您猜的数字太大了");
            } catch (Exception e) {
                System.out.println("无效的输入,请重试。");
            }
        }
    }
}
英文:

If you want to parse your number you can use Integer.parseInt() method as shown. Also you are using hasNextInt() incorrectly. You are also not storing in.next() value in any variable.

import java.util.Scanner;

public class GuessNumber {
 public static void main(String[] args) {
    int num = (int)(Math.random() * 101);

    Scanner input = new Scanner(System.in);
    System.out.println(&quot;Welcome to my Guessing Game!&quot;);

    int guess = -1;
    //Loop goes as long as guess doesn&#39;t equal num
    while (true) {
        System.out.print(&quot;Guess a number between 1 and 100:   &quot;);
        String numberString = input.nextLine();

        //Validates input
        try {
            guess = Integer.parseInt(numberString);
            if (guess == num) {
                System.out.println(&quot;Correct!&quot;);
                break;
            } else if (guess &lt; num)
                System.out.println(&quot;Your guess was too low&quot;);
            else
                System.out.println(&quot;Your guess was too high&quot;);
        } catch (Exception e) {
            System.out.println(&quot;Invalid response, try again.&quot;);
        }
       }
    }
}

huangapple
  • 本文由 发表于 2020年9月5日 23:10:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63755397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定