Hi i am trying to print 6 numbers randomly..however i do not want any of the numbers to be a duplicate. Could you help me see what is wrong with my codes/flow? as i am still getting duplicates. Thanks in advance.
Code:boolean donPrintFlag = false; //create random object to use Random r2 = new Random(); //create array object int arrayNum[] = new int[7]; for (int i = 0; i < 6; i++) { int randNum3 = r2.nextInt(45) + 1; //initialise 6 numbers into array arrayNum[i] = randNum3; // System.out.println("array print " + arrayNum[i]); //loop through all numbers to find duplicate for (int j = 1; j < 6; j++) { if (arrayNum[i] == arrayNum[j]) { donPrintFlag = true; } else{ donPrintFlag = false; } } //print non duplicate numbers if (!donPrintFlag) { System.out.println("Number" + i + ": " + arrayNum[i]); }




Reply With Quote