mardi 30 octobre 2018

Java - printing all random numbers at the last line of the while loop

I have the while loop running and processing properly. When the numbers that the user entered do not match the random number, the code prints out "Sorry ...(and proceeds to explain what the right number is)" each time the user is wrong. However, I cannot get the code to print out those same exact random numbers at the very end of the last loop before it terminates. Any suggestions?

   while (counter < 6)
        {
          counter++;
          System.out.println("Enter a number between 1-60: ");
          userInput = scan.nextInt();
          if (userInput > 60 || userInput < 1)
            System.out.println("Invalid input");

          int randomNumber = (int) (Math.random() * 60) + 1;
          if (userInput == randomNumber)
            System.out.println("Congrats, you have won!");
        else
          System.out.println("Sorry, you didn't choose the winning number." + "The winning number is " + randomNumber + ".");
        }

The bottom of the code has the winning number, but I want all of those same exact random numbers (which were randomized earlier) to show up at the end of the sixth loop. Also, the order of the user input does not influence the results. If the user chooses 1-13-8-34-56-2 and the computer had come up with 1-8-56-2-14-34…there would still be 5 matching numbers




Aucun commentaire:

Enregistrer un commentaire