fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Random r = new Random();
  13. // repete o loop até gerar um array com números repetidos
  14. while (true) {
  15. int numero;
  16. int[] num = new int[6];
  17. for (int i = 0; i < num.length; i++) {
  18. numero = r.nextInt(60) + 1;
  19. for (int j = 0; j < num.length; j++) {
  20. if (numero == num[j] && j != i) {
  21. numero = r.nextInt(60) + 1;
  22. } else {
  23. num[i] = numero;
  24. }
  25. }
  26. }
  27. if (temRepetidos(num)) { // encontrei, imprime o array e encerra o while
  28. System.out.println("Ops, tem números repetidos");
  29. for (int n : num) {
  30. System.out.print(n + " ");
  31. }
  32. break;
  33. }
  34. }
  35. }
  36.  
  37. // verifica se tem números repetidos
  38. static boolean temRepetidos(final int[] numeros) {
  39. Set<Integer> set = new HashSet<Integer>();
  40. for (int i : numeros) {
  41. if (set.contains(i))
  42. return true;
  43. set.add(i);
  44. }
  45. return false;
  46. }
  47. }
Success #stdin #stdout 0.09s 48520KB
stdin
Standard input is empty
stdout
Ops, tem números repetidos
23  5  1  23  3  38