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