我正在尝试编写一个 if/else 来测试用户输入的整数。如果他们输入一个 int,程序就会继续。如果他们输入任何其他内容,程序会生成一条错误消息,要求输入正确。这是在球场的任何地方吗?
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int [] foo;
foo = new int[3];
foo[0]=1;
foo[1]=2;
foo[2]=3;
System.out.print("Make a choice between 0 and 2: ");
int itemChoice = keyboard.nextInt();
if (itemChoice != foo) {
System.out.print("Not a valid choice");
}
else {
System.out.print("Valid choice. You picked " + itemChoice);
}
}
}
我收到此错误:
required: int
found: boolean
test.java:17: error: incomparable types: int and int[]
if (itemChoice != foo) {