JAVA顺序查找

 1 import java.util.Scanner;
 2 public class InOrderToFind{
 3     public static void main(String[] args) {
 4         Scanner input= new Scanner (System.in);
 5         System.out.println("请输入您想输入数字的总个数:");
 6         int n=input.nextInt();//n用来保存数组内数字的个数
 7         int []arry=new int [n];
 8         System.out.println("请输入您想输入数字:");
 9         for(int i=0;i<n;i++) {
10             arry[i]=input.nextInt();
11         }
12         System.out.println("请输入您想查询的数字:");
13         int num=input.nextInt();//输入要查询的数字
14         int count=0;//计数器
15         int []b=new int[n];//和count结合,记录每次出现的下标
16         b[count]=-1;//假定该数出现的位置下标,若为-1,则没出现
17         for(int i=0;i<n;i++) {
18             if(arry[i]==num) {
19                 b[count]=i;
20                 count++;
21             }
22         }
23         if(b[count]!=-1) {
24             System.out.println("已经找到你要查询的数值:"+num+",该数共出现"+count+"次");
25             System.out.printf("该数在数组从左向右第");
26             for(int i=0;i<count;i++) {
27                 System.out.printf(" %d个 ",b[i]+1);
28             }
29             System.out.println();
30         }
31         else {
32             System.out.println("抱歉没有找到你要查询的数!");
33         }
34     }
35 
36 }

 

转载于:https://www.cnblogs.com/HuangTangBlog/p/10651815.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值