java 数组初级题

来源:百度知道 编辑:UC知道 时间:2024/06/30 03:52:21
8.4.2.1.23.344.12 7个数为一数列
求:从键盘输入一个数字 判断该数字是否在数列中。
越简单越好 我是初学者。。 谢谢哈

public void test() throws IOException{

int[] arr = new int[]{8,4,2,1,23,344,12,7};
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
try {
int x = Integer.parseInt(in.readLine);
} catch(IOException e) {
System.out.println(e);
}

boolean flag = false;
for(int i=0; i<arr.length; i++) {
if (x == arr[i]) {
flag = true;
break;
}
}

if (flag) {
System.out.println("存在");
}else {
System.out.println("不存在");
}
}