首页 > 编程语言 > 详细

JAVA顺序查找

时间:2019-04-03 22:10:21      阅读:101      评论:0      收藏:0      [点我收藏+]
 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 }

 

JAVA顺序查找

原文:https://www.cnblogs.com/HuangTangBlog/p/10651815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!