Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package guessinggame;
- import java.util.Scanner;
- public class GuessingGame {
- public static void giveHighHint() {
- System.out.println("Guess a higher number.");
- }
- public static void giveLowHint() {
- System.out.println("Guess a lower number.");
- }
- public static int randomNumber(int randomNum) {
- randomNum = (int)(20 * Math.random() + 1);
- return(randomNum);
- }
- public static void main(String[] args) {
- int randomNum = 0, guess = 0;
- Scanner input = new Scanner(System.in);
- randomNum = randomNumber(randomNum);
- while (guess != randomNum) {
- System.out.print("Guess a number between 1 & 20: ");
- guess = input.nextInt();
- if (guess > randomNum) {
- giveLowHint();
- }
- else if (guess < randomNum) {
- giveHighHint();
- }
- }
- System.out.print("You guessed the right number!");
- }
- }
Add Comment
Please, Sign In to add comment