import java.util.Scanner;
import java.util.Arrays;
//编写程序实现从一个数组中查找指定的数据。查找成功,显示出所在数组中的位置;
//查找不成功,给出提示信息。若数组定义为int[]a={1,9,15,17,27,55,68,34,93,45};
public class 一03 {
public static void main(String[] args) {
int[]a={1,9,15,17,27,55,68,34,93,45};
System.out.println("请输入您想查的数字:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int count=0;
for(int i=0;i<=a.length;i++){
if(a[i]==n){
System.out.println("查找成功,此数字在数组中的位置为:"+count);
}else{
count++;}
if(count==a.length){
System.out.println("查找失败,数组中没有您想查找的数字");
//写到这里脑子里在想到底怎么样才可以写出查找失败的语句,但是没有想到用count变量。
}
}
}
}
12-22
328
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交