Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void problem_2() {
- /*
- Write a Java program that would ask the user to enter five numbers.
- As the user enters the five numbers the program will make sure that
- all the numbers entered by the user are unique. For example, if the
- user enters 2, 3, 4, 6, and then tries to enter 3 again the program
- will display that 3 is already among the entered numbers and would
- ask the user to enter a new number. You will have to keep taking
- numbers from the user until you receive five unique numbers. At the
- end, print those five unique numbers.
- */
- Scanner tuna = new Scanner(System.in);
- int n, i, j;
- int[] arr = new int[5];
- //number = tuna.nextInt();
- System.out.println("Enter 5 numbers:");
- for (i = 0; i < 5; i++) {
- arr[i] = tuna.nextInt();
- n = arr[i];
- for (j = 0; j <= i - 1; j++) {
- if (arr[j] == n) {
- System.out.println(n + " is already among the entered numbers. Enter a new number");
- i--;
- }
- }
- }
- for (i = 0; i < 5; i++) {
- System.out.print(arr[i] + " ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement