Advertisement
Guest User

MAIN

a guest
Nov 12th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. public static void problem_2() {
  2. /*
  3. Write a Java program that would ask the user to enter five numbers.
  4. As the user enters the five numbers the program will make sure that
  5. all the numbers entered by the user are unique. For example, if the
  6. user enters 2, 3, 4, 6, and then tries to enter 3 again the program
  7. will display that 3 is already among the entered numbers and would
  8. ask the user to enter a new number. You will have to keep taking
  9. numbers from the user until you receive five unique numbers. At the
  10. end, print those five unique numbers.
  11. */
  12. Scanner tuna = new Scanner(System.in);
  13. int n, i, j;
  14. int[] arr = new int[5];
  15. //number = tuna.nextInt();
  16. System.out.println("Enter 5 numbers:");
  17.  
  18. for (i = 0; i < 5; i++) {
  19. arr[i] = tuna.nextInt();
  20. n = arr[i];
  21.  
  22. for (j = 0; j <= i - 1; j++) {
  23. if (arr[j] == n) {
  24. System.out.println(n + " is already among the entered numbers. Enter a new number");
  25. i--;
  26. }
  27. }
  28.  
  29. }
  30.  
  31. for (i = 0; i < 5; i++) {
  32. System.out.print(arr[i] + " ");
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement