vendredi 10 janvier 2020

How do I create a loop that will generate random numbers until it matches my user input?

I'm very new at java. I'm doing an exercise and I am stuck. Basically I have to

  1. Ask the user to pick a random # from 1-100. Ask again if not between those #.
  2. Generate random # between 1-100 until it matches the user input.
  3. Print matching number and number of tries to get matching number.

So far, I've done this:

       import java.util.Scanner;
import java.util.Random;

public class RoughNums{
   public static void main(String[]args){
      Scanner reader = new Scanner(System.in);
      Random rand = new Random();
      int rantInt = rand.nextInt(101);

         while(true) {

           System.out.println("Hello friend! Pick a number between 1 - 100: ");
          int pick = Integer.parseInt(reader.nextLine());

          if(pick > 100){
          System.out.println("Oops, number too big! Please try again!\n");

                  }
          else {
          System.out.println("Let's see if I can get the same number too!");


          break;}

          }      


      }
      }

I'm not sure where to go from here. Should I add another while loop? If so, how should I write it? How do I generate random numbers until it matches the user input number?




Aucun commentaire:

Enregistrer un commentaire