Java 需要验证int;hasNextInt不是';行不通
我正在做一个家庭作业,做一个猜谜游戏。我已经让那部分工作了,但我们必须验证输入。我尝试过使用hasNextInt,但我一直收到一个错误,它说“int不能被解除引用”,并指向“!guess.hasNextInt”代码 我尝试了很多次迭代,但仍然得到相同的错误。我包含的代码只是我最近的一次尝试 如何让hasNextInt工作,或者如何验证输入Java 需要验证int;hasNextInt不是';行不通,java,Java,我正在做一个家庭作业,做一个猜谜游戏。我已经让那部分工作了,但我们必须验证输入。我尝试过使用hasNextInt,但我一直收到一个错误,它说“int不能被解除引用”,并指向“!guess.hasNextInt”代码 我尝试了很多次迭代,但仍然得到相同的错误。我包含的代码只是我最近的一次尝试 如何让hasNextInt工作,或者如何验证输入 import java.util.Scanner; public class GuessNumber { public static void ma
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");
}
}
}
import java.util.Scanner;
公共类猜测数{
公共静态void main(字符串[]args){
int num=(int)(Math.random()*101);
扫描仪输入=新扫描仪(System.in);
System.out.println(“欢迎来到我的猜谜游戏!”);
int guess=-1;
//只要guess不等于num,循环就会继续
while(猜测!=num){
打印(“猜一个介于1和100之间的数字:”;
guess=input.nextInt();
//验证输入
而(!guess.hasnetint()){
System.out.println(“无效响应,请重试”);
in.next();
}
如果(猜测==num)
System.out.println(“正确!”);
else if(猜测
我修复了代码:
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();
}
}
}
publicstaticvoidmain(字符串[]args){
int num=(int)(Math.random()*101);
系统输出打印项数(num);
扫描仪输入=新扫描仪(System.in);
System.out.println(“欢迎来到我的猜谜游戏!”);
int guess=-1;
//只要guess不等于num,循环就会继续
while(猜测!=num){
打印(“猜一个介于1和100之间的数字:”;
guess=input.nextInt();
如果(猜测==num){
System.out.println(“正确!”);
中断;}
else if(猜测
如果用户猜到数字,游戏将使用break
在while(!guess.hasnetint())
中,您使用了一个整数,在该整数中,扫描器输入
我修复了代码:
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();
}
}
}
publicstaticvoidmain(字符串[]args){
int num=(int)(Math.random()*101);
系统输出打印项数(num);
扫描仪输入=新扫描仪(System.in);
System.out.println(“欢迎来到我的猜谜游戏!”);
int guess=-1;
//只要guess不等于num,循环就会继续
while(猜测!=num){
打印(“猜一个介于1和100之间的数字:”;
guess=input.nextInt();
如果(猜测==num){
System.out.println(“正确!”);
中断;}
else if(猜测
如果用户猜到数字,游戏将使用break
在
while(!guess.hasNextInt())
中,您使用的是一个整数,其中需要扫描器输入如果您想解析数字,可以使用integer.parseInt()方法,如图所示。此外,您还错误地使用了hasnetint()。您也没有在任何变量中存储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("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.");
}
}
}
}
import java.util.Scanner;
公共类猜测数{
公共静态void main(字符串[]args){
int num=(int)(Math.random()*101);
扫描仪输入=新扫描仪(System.in);
System.out.println(“欢迎来到我的猜谜游戏!”);
int guess=-1;
//只要guess不等于num,循环就会继续
while(true){
打印(“猜一个介于1和100之间的数字:”;
String numberString=input.nextLine();
//验证输入
试一试{
guess=Integer.parseInt(numberString);
如果(猜测==num){
System.out.println(“正确!”);
打破
}else if(猜测
如果要解析数字,可以使用Integer.parseInt()方法,如图所示。此外,您还错误地使用了hasnetint()。您也没有在任何变量中存储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("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.");
}
}
}
}
import java.util.Scanner;
公共类猜测数{
公共静态void main(字符串[]args){
int num=(int)(Math.random()*101);
扫描仪输入=新扫描仪(System.in);
System.out.println(“欢迎来到我的猜谜游戏!”);
int guess=-1;
//只要guess不等于num,循环就会继续
while(true){
打印(“猜一个介于1和100之间的数字:”;
String numberString=input.nextLine();
//验证输入
试一试{
guess=Integer.parseInt(numberString);
如果(猜测==num){
System.out.println(“正确!”);
打破
}else if(猜测
变量guess
属于原始类型int
。所以你不能打电话