Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.util.Scanner;

public class Unique
{
// gets 5 unique numbers from the user
public static void main(String args[]) {
{
Scanner input = new Scanner( System.in );

/* Create an array of five elements*/ 
int count = 0; // number of uniques read
int entered = 0; // number of entered numbers

while( entered < numbers.length )
{
System.out.print( "Enter number: " );
/* Write code here to retrieve the input from the user */

// validate the input
/* Write an if statement that validates the input */ 
{
// flags whether this number already exists
boolean containsNumber = false;

// increment number of entered numbers
entered++;

/* Compare the user input to the unique numbers in the array using a for 
statement. If the number is unique, store new number */

/* add the user input to the array only if the number is not already 
in the array */
if ( !containsNumber )
{
/* Write code to add the number to the array and increment 
unique items input */
} // end if
else 
System.out.printf( "%d has already been entered\n",
number );
} // end if
else
System.out.println( "number must be between 10 and 100" );

// print the list of unique values
/* Write code to output the contents of the array */
} // end while 
} // end method Main
} // end class Unique




Here's the sample output:

Enter a number in [10,100]: 11
11
Enter a number in [10,100]: 22
11 22
Enter a number in [10,100]: 33
11 22 33
Enter a number in [10,100]: 11
11 has already been entered
11 22 33
Enter a number in [10,100]: 44
11 22 33 44


Enter a number in [10,100]: 10
10
Enter a number in [10,100]: 30
10 30
Enter a number in [10,100]: 5
number must be between 10 and 100
10 30
Enter a number in [10,100]: 50
10 30 50
Enter a number in [10,100]: 70
10 30 50 70
Enter a number in [10,100]: 100
10 30 50 70 100


What I have tried:

I'm trying to figure out it for a long time but still can't work...Can someone help me please? I need it immediately...
Posted
Updated 23-May-18 7:29am
v3

1 solution

Run it and figure out what the first error is. Fix it, then go to the next error and fix that. Continue this process until you are done.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900