import java.util.Scanner;
public class methodTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {1, 2, 44, 68, 45, 99, 34, 76, 46, 86, 32, 35};
System.out.print("请输入要判断是否存在的数:");
int x = sc.nextInt();
if (judge(x,arr)){
System.out.println("存在");
}else System.out.println("不存在");
}
public static boolean judge(int a, int[] arr) {
boolean flag = false;
for (int i = 0; i < arr.length - 1; i++) {
if (a == arr[i]) {
return true;
}
}
return flag;
}
}
定义一个方法判断数组中某一个数是否存在(方法学习记录)
于 2022-10-26 20:25:54 首次发布