dimanche 30 avril 2017

Number Guessing Program issue Java

I'm having trouble with this assignment that I have been given(High School). It's a number guessing game and I have already gotten most of it down, but he wants us to put a range of numbers on the console. An example output would be:

Enter lower limit: 4

Enter Upper limit: 10

Etc, basically choose the specific range of numbers you want the computer to choose from. I have only been able to code in a set range(1-1000) since I can't figure out how to do what he wants. Here is my code:

import java.util.Scanner;


public class Game {
  public static void main(String[] args) {
        int randomNumber;
        randomNumber = (int) (Math.random() * 999 + 1);           
        Scanner keyboard = new Scanner(System.in);
        int guess;
 do {
            System.out.print("Enter a guess (1-1000): ");
            guess = keyboard.nextInt();

    if (guess == randomNumber)
  System.out.println("Your guess is correct. Congratulations!");
    else if (guess < randomNumber)
       System.out.println("Your guess is smaller than the secret number.");
  else if (guess > randomNumber)
 System.out.println("Your guess is greater than the secret number.");
        } while (guess != randomNumber);
  }

}

If you try it it is also really hard to play anyway. I would appreciate some help, thanks!

Aucun commentaire:

Enregistrer un commentaire