海阔天空

当前时间为:
欢迎大家来到海阔天空https://www.9713job.com,广告合作以及淘宝商家推广请微信联系15357240395

2020java教程:查找数组中的元素是否存在

未分类
2020-08-07 18:11:59
1822677238@qq.com

手机扫码查看

2020java教程:查找数组中的元素是否存在

查找数组中的元素是否存在

public class demos {
static int[] arr={9,7,6,4,3,1,2,5,8};
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println(“请输入你要查找的值”);
int n=sc.nextInt();
int result=find(n);
if(result>=0){
System.out.println(“你要查找的值的下标为” + result);
}else{
System.out.println(“没有找到你要查找的值”);
}
}
public static int find(int n){
int index=0;
for(int i=0;i<arr.length;i++){
if(arr[i]==n){
index=i;
return index;
}
}
return -1;
}
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注