英文:
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("Welcome to my Guessing Game!");
int guess = -1;
//Loop goes as long as guess doesn't equal num
while (guess != num) {
System.out.print("Guess a number between 1 and 100: ");
guess = input.nextInt();
//Validates input
while (!guess.hasNextInt()) {
System.out.println("Invalid response, try again.");
in.next();
}
if (guess == num)
System.out.println("Correct!");
else if (guess < num)
System.out.println("Your guess was too low");
else
System.out.println("Your guess was too high");
}
}
}
</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("Welcome to my Guessing Game!");
int guess = -1;
//Loop goes as long as guess doesn't equal num
while (guess != num) {
System.out.print("Guess a number between 1 and 100: ");
guess = input.nextInt();
if (guess == num) {
System.out.println("Correct!");
break;}
else if (guess < num)
System.out.println("Your guess was too low");
else
System.out.println("Your guess was too high");
//Validates input
while (!input.hasNextInt()) {
System.out.println("Invalid response, try again.");
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("Welcome to my Guessing Game!");
int guess = -1;
//Loop goes as long as guess doesn't equal num
while (true) {
System.out.print("Guess a number between 1 and 100: ");
String numberString = input.nextLine();
//Validates input
try {
guess = Integer.parseInt(numberString);
if (guess == num) {
System.out.println("Correct!");
break;
} else if (guess < num)
System.out.println("Your guess was too low");
else
System.out.println("Your guess was too high");
} catch (Exception e) {
System.out.println("Invalid response, try again.");
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论