-1

I'm new to programming and I'm making a guessing game where the program randomly generates a number between 1 and 10, the user then is asked to guess what the number is, the user should be able to keep guessing until he guesses correctly and the system asks them if they want to play again, In my code I've printed the number that the system has randomly generated so that it is quicker to complete the game whilst testing. When I try and execute the program and enter the number that the system has generated the message that they are correct and asking if they want to play again does not come up.

Any help would be greatly appreciated! Thank you in advance,

(Also, anything wrong with this question just tell me, it's my first time asking on here)

Here is my code,

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

public class GuessingGame1 {
    public static int randomizer() {
        Random rand = new Random();
        int num = rand.nextInt(10)+1;
        System.out.println(num);
        int count = 0;
        return num;
    }
    public static int userInput() {
        System.out.println("I've thought of a number between 1 and 10");
        System.out.println("Enter your guess...");
        Scanner scan = new Scanner(System.in);
        int guess = scan.nextInt();
        return guess;
    }
    public static String compare() {
        int count = 0;
        String result = null;
        if (userInput() == randomizer()) {
             System.out.println("You guessed it - I was thinking of " + randomizer());
             count++;
             result = "It took you " + count + " guesses.";
             return result;
        }

        else if (userInput() > randomizer())  {
             result = "Lower!";
             count++;
             return result;
        }

        else if (userInput() < randomizer()) {
             result = "Higher";
             count++;
        }
        return result;

    }
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        Scanner scanLine = new Scanner(System.in);

        String playAgain = "";

        do  {
            randomizer();

            do {
                userInput();
                compare

            } while (userInput() != randomizer());
            System.out.println("Play again? Yes/No");
            playAgain = scanLine.nextLine();


        } while (playAgain.equalsIgnoreCase("yes") || playAgain.equalsIgnoreCase("y"));
    }
}
  • 1
    Asking for debugging assistance is not a question or why this code isn't working either. See [How to ask](https://stackoverflow.com/help/how-to-ask). – Neijwiert Mar 30 '18 at 05:15
  • 1
    Each time you call `randomizer()` you get a new random number. – Ken Y-N Mar 30 '18 at 05:16
  • Take a debugger, go through the code line by line, and determine what the issue is. If that doesn’t help then explain what happens and where. – Sami Kuhmonen Mar 30 '18 at 05:17

3 Answers3

0

The problem is that you call twice to Randomizer!

call randomizer once as parameter to compare and return boolean from compare for a match.

Gal Nitzan
  • 391
  • 2
  • 12
0

You must change your methods something like this

   public static String compare(int a,int b) {
        int count = 0;
        String result = null;
        if (a == b) {
             System.out.println("You guessed it - I was thinking of " + b);
             count++;
             result = "It took you " + count + " guesses.";
             return result;
        }

        else if (a > b)  {
             result = "Lower!";
             count++;
             return result;
        }

        else if (a < b) {
             result = "Higher";
             count++;
        }
        return result;

    }
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        Scanner scanLine = new Scanner(System.in);

        String playAgain = "";
        int a;
        int b;
        do  {
            do {
               a=userInput();
               b= randomizer();
               System.out.println(compare(a,b));
            } while (a != b);
            System.out.println("Play again? Yes/No");
            playAgain = scanLine.nextLine();


        } while (playAgain.equalsIgnoreCase("yes") || playAgain.equalsIgnoreCase("y"));
    }
}
0

you have left the () in compare and the count will always be zero as it is been initialized when the compare function is called.