未解析java变量/用户输入
好的,这里是:
我询问所选的号码
然后询问绘制的数字
然后,当我转到所以如果他们匹配它告诉我,pickOne无法解决,但我使用的是用户的输入,我做错了什么
我一眼就搜索了很多东西,似乎都没有发现同样的问题。因此,如果我不善于找到答案(如果答案已被回答),我很抱歉。我搜索了大约30分钟
代码:
import java.util.Scanner;
import java.util.*;
public class CheckNumber {
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
int matchOne;
int matchTwo;
int matchThree;
int matchFour;
/*
*
********** THE NUMBERS CHOSEN BY USER **********
*
*/
System.out.println("First number picked: ");
if (userInput.hasNextInt()){
int pickOne = userInput.nextInt();
}
System.out.println("Second number picked: ");
if (userInput.hasNextInt()){
int pickTwo = userInput.nextInt();
}
System.out.println("Third number picked: ");
if (userInput.hasNextInt()){
int pickThree = userInput.nextInt();
}
System.out.println("Fourth number picked: ");
if (userInput.hasNextInt()){
int pickFour = userInput.nextInt();
}
/*
*
********** THE WINNING NUMBERS DRAWN **********
*
*/
System.out.println("First number drawn: ");
if (userInput.hasNextInt()){
int drawnOne = userInput.nextInt();
}
System.out.println("First number drawn: ");
if (userInput.hasNextInt()){
int drawnTwo = userInput.nextInt();
}
System.out.println("First number drawn: ");
if (userInput.hasNextInt()){
int drawnThree = userInput.nextInt();
}
System.out.println("First number drawn: ");
if (userInput.hasNextInt()){
int drawnFour = userInput.nextInt();
}
/*
********** MATCHING NUMBERS **********
*/
问题出在这里:
if (pickOne == drawnOne){
System.out.println(pickOne + " is a match!");
}
}
}
# 1 楼答案
块中定义的变量只能从块中访问。在您的程序中,
pickedOne
,pickedTwo
drawOne
,drawTwo
。。。。变量仅限于声明它们的if
块。因此,如果您试图在那些if
条件之外使用它们,您将得到错误消息,cannot find symbol
在
main()
函数中声明以下变量,而不是在if
条件中声明我强烈鼓励你阅读scope of variables in Java
# 2 楼答案
输入和输出是什么。举几个例子。我看不出if(userInput.hasNextInt())有什么好用
你是说像这样吗