Unique Number in java
The below is one of the example of unique number code which mean that the number of input must be unique than the input number before.Below is one of the example source code!
Copy to IDE for better view!
public static void unique(int[] list,int key){
Scanner scanner=new Scanner(System.in);
int k=0; //the number of value k use to indicate the time of repeated number
//Search the number in the array list whether got repeated
for(int i=0;i<list.length;++i){
//if there are 1 similar the return values "k" plus 1
if (key==list[i]){k++;}
//if the k is more or equal to two mean got repeated input one time
if(k>=2){
System.out.println("The number "+list[i]+" is repeated");
System.out.println("The number that is already key in are ");
//print out the values that is already key in
for (int j=0;j<i;j++){
System.out.print(list[j]+" ");
}
System.out.print("\nPlease enter a new number:");
k--;/* since after the value enter the list[i] will be replace by new value thus it must reduce!*/
list[i]=scanner.nextInt();
unique(list,list[i]);/*loop again the method to check weather the number is just enter go repeated or not!*/
}
}
Comments
Post a Comment