Program1-1

package com.algo;

import java.util.Random;
import java.util.Scanner;

public class P1_1 {
	static int N=20;
	public static void main(String[] args){
		int[] arr = new int[N];
		int x,i;
		int f = -1;
		
		Random r = new Random();
		for(i=0; i<N; i++){
			arr[i]=r.nextInt(100);
		}
		
		System.out.println("随机生成的数据序列:");
		for(i=0; i<N; i++){
			System.out.print(arr[i]+" ");			
		}
		System.out.println();
		System.out.println();
		System.out.println("输入要查找的整数:");
		Scanner input = new Scanner(System.in);
		x=input.nextInt();
		
		for(i=0; i<N; i++){
			if(x==arr[i]){
				f=i;
				break;
			}
		}
		if(f<0){
			System.out.println("没有找到数据:"+x);
		}else{
			System.out.println("数据:"+x+" 位于数组的第  "+(f+1)+" 个元素处\n");
		}
		
	}

}
/*
随机生成的数据序列:
64 35 61 51 83 11 4 35 7 34 90 34 32 98 95 53 66 9 90 72 

输入要查找的整数:
11
数据:11 位于数组的第  6 个元素处
*/

posted @ 2016-01-27 14:37  phlsheji  阅读(206)  评论(0编辑  收藏  举报