+4 votes
by (13.6m points)

I'm creating a high/low guessing game as part of a study assignment, and the part im stuck at is getting the amount of guesses returned to the main method. We have specifically been told that the main method has to print the number of guesses, while the method "playGame" actually does the playing.

There's more to the code, a method called giveReponse that checks if the number is correct or too high/low, but it works as intended. I get "Cannot find symbol" when trying to print how many guesses it took to complete the game. If it wasn't so important to print it in the main method I'd print the amount in the method playGame, but thats a no-go. What am I doing wrong? The code looks like this:

public class HiLo {
public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Welcome to classic guessing game of high/low!");

       String difficulty = scan.next();

          if (difficulty.equals("easy")) {               
             playGame(10);
          } else if (difficulty.equals("medium")) {
             playGame(100);
          } else if (difficulty.equals("hard")) {
             playGame(1000);
          } 

          System.out.println("You won in" + guesses + "attempts.");
   }//EndsMain

       public static int playGame(int maxNumber) {
            Scanner scan = new Scanner(System.in); 
            int rannr = (int)(Math.random() * maxNumber) +1; 
            int answer = rannr;
            int guess = 0;
            int guesses = 0;

            System.out.println("Game is starting...");

            do {
            guess = scan.nextInt();      
            guesses ++;                   
            giveResponse(answer, guess); 

               if (answer == guess) {
                 break;
               }     
            } while (answer != guess);

       return guesses;
   } //Ends playGame

3 Answers

+8 votes
by (13.6m points)

Your method playGame( ) is returning a value but since is not assigned to no variable, those returns are getting lost... additional to that it looks like the code is not complete:

this statement is not going to let you compile:

System.out.println("You won in" + guesses + "attempts.");

because the only guesses variable I see in there is scoped in the playGame method ....

do instead something like:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Welcome to classic guessing game of high/low!");
    int guesses = 0;
    String difficulty = scan.next();

    if (difficulty.equals("easy")) {
        guesses = playGame(10);
    } else if (difficulty.equals("medium")) {
        guesses = playGame(100);
    } else if (difficulty.equals("hard")) {
        guesses = playGame(1000);
    }
+3 votes
by (13.6m points)

The problem is at:

System.out.println("You won in" + guesses + "attempts.");

Since the variable guesses is not defined in the main method, the compiler doesn't know what you are referencing to with that symbol.

But since playGame() returns the number of guesses, I'd recommend something like this:

if (difficulty.equals("easy")) {               
         System.out.println("You won in" +playGame(10)+ "attempts.");
      } else if (difficulty.equals("medium")) {
         System.out.println("You won in" +playGame(100)+ "attempts.");
      } else if (difficulty.equals("hard")) {
         System.out.println("You won in" +playGame(1000)+ "attempts.");
      } 
+2 votes
by (13.6m points)

Here's a solution that shows how easy it would be to restrict guesses. Not much extra effort or thought:

package games;

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

/**
 * HiLo guessing game
 * Created by Michael
 * Creation date 4/9/2016.
 * @link https://stackoverflow.com/questions/36522303/java-creating-hi-low-game-using-multiple-methods-stuck-at-returning-no-of-gu
 */
public class HiLo {

    public static void main(String [] args) {
        int maxValue = (args.length > 0) ? Integer.parseInt(args[0]) : 100;
        int maxGuesses = (args.length > 1) ? Integer.parseInt(args[1]) : 5;
        Scanner scanner = new Scanner(System.in);
        String answer = "Y";
        do {
            play(scanner, maxValue, maxGuesses);
            System.out.println("Play again? [Y/N]: ");
            answer = scanner.next();
            System.out.println(String.format("You answered %s; let's play again!", answer));
        } while ("Y".equalsIgnoreCase(answer));
    }

    private static void play(Scanner scanner, int maxValue, int maxGuesses) {
        int value = new Random().nextInt(maxValue) + 1;
        int numGuesses = 0;
        boolean match = false;
        do {
            System.out.println(String.format("Guess a value between 1 and %d: ", maxValue));
            int guess = Integer.parseInt(scanner.next());
            if (guess < value) {
                System.out.println(String.format("Too low; guess again. (%d guesses left)", (maxGuesses-numGuesses-1)));
            } else if (guess > value) {
                System.out.println(String.format("Too high; guess again (%d guesses left)", (maxGuesses-numGuesses-1)));
            } else {
                match = true;
                System.out.println(String.format("You got it right in %d guesses! ", numGuesses+1));
                break;
            }
        } while (!match && ++numGuesses < maxGuesses);
        if (!match) {
            System.out.println(String.format("The correct answer was %d; you're only allowed %d guesses.  Better luck next time!", value, maxGuesses));
        }
    }
}

Related questions

...