Search for numbers
Generate 20 integers at random and enter the data to be searched;
Compare data one by one. When the data is equal, locate the data and output the data location.
The Code is as follows:
Package com. Xu. Main;
Import java. util. Random;
Import java. util. collections;
Public class p1_1 {
/**
* @ Function: Search for numbers
* @ Author:
* @ Date: 2012-10-14
*/
Static int n = 20;
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Int [] arr = new int [20];
Int X, I;
Int F =-1;
Random r = new random ();
// Random Seed
For (I = 0; I <n; I ++)
// Generate a random number
{
Arr [I] = R. nextint (100 );
}
System. Out. println ("randomly generated data sequence :");
For (I = 0; I <n; I ++)
{
System. Out. Print (ARR [I] + "");
// Output sequence
}
System. Out. println ("\ n ");
System. Out. println ("Enter the integer to be searched :");
S = new partition (system. In );
X = S. nextint ();
// Enter the number to be searched
For (I = 0; I <n; I ++)
// Sequential search
{
If (x = arr [I])
// Locate the data
{
F = I;
Break;
}
}
If (F <0)
{
System. Out. println ("no data found:" + x );
}
Else
{
System. Out. println ("data:" + x + "in the array" + (F + 1) + "element .");
}
}
}
Running result: