好的,所以这里是:
我要求提取的数字
然后询问抽出的数字
然后当我转到它,如果他们匹配它告诉我 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!");
}
}
}
答案 0 :(得分:0)
输入和输出是什么。举一些例子。我没有好好利用 if(userInput.hasNextInt())
你的意思是这样吗?
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;
int pickOne;
int pickTwo;
int pickThree;
int pickFour;
int drawnOne;
int drawnTwo;
int drawnThree;
int drawnFour;
/*
********** THE NUMBERS CHOSEN BY USER **********
*/
System.out.println("First number picked: ");
pickOne = userInput.nextInt();
System.out.println("Second number picked: ");
pickTwo = userInput.nextInt();
System.out.println("Third number picked: ");
pickThree = userInput.nextInt();
System.out.println("Fourth number picked: ");
pickFour = userInput.nextInt();
/*
*
********** THE WINNING NUMBERS DRAWN **********
*
*/
System.out.println("First number drawn: ");
drawnOne = userInput.nextInt();
System.out.println("Second number drawn: ");
drawnTwo = userInput.nextInt();
System.out.println("Third number drawn: ");
drawnThree = userInput.nextInt();
System.out.println("Fourth number drawn: ");
drawnFour = userInput.nextInt();
/*
********** MATCHING NUMBERS **********
*/
if (pickOne == drawnOne) {
System.out.println(pickOne + " is a match!");
}
}
}
答案 1 :(得分:0)
块中定义的变量只能从块中访问。在您的计划中,pickedOne
,pickedTwo
,......,drawOne
,drawTwo
,......变量的范围仅限于{{1}你声明它们的块。因此,如果您尝试在if
条件之外使用它们,则会收到错误消息if
。
在cannot find symbol
函数内声明以下变量,而不是在main()
条件内。
if
我强烈建议你阅读scope of variables in Java。