如题所述
可以把数组转成list,利用list的contains方法来判断
Integer[] arr = new Integer[] { 1, 2, 3, 4 };
List<Integer> list = Arrays.asList(arr);
if (list.contains(5)) {
System.out.println("包含");
} else {
System.out.println("不包含");
}
public class Test {
public static void main(String[] args) {
boolean flag = false;
int[] is = new int[1000];
for (int i = 0; i < is.length; i++) {
is[i] = i;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数");
int num = scanner.nextInt();
for (int i : is) {
if (num == i) {
flag = true;
}
}
if (flag) {
System.out.println("有这个整数");
} else {
System.out.println("没有这个整数");
}
}
}
温馨提示:答案为网友推荐,仅供参考