-1

I know this same thing has been asked before but I am having a lot of trouble getting this guy to work. EDIT: it now seems to just keep running indefinitely. I no longer get an error but the code will just keep running until I stop it.

And now for my code:

import java.util.Scanner;

public class Guess2
{

    public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            Scanner outScan = new Scanner (System.in);
            boolean input = true;
            while (input == true) {

            System.out.println(
                    "Gimme a number, any number. This number will be the maximum\n"
                            + " in a range of numbers. Your goal is to guess\n"
                            + " the number that I have picked at random"
                            + " within this range. Then, enter how many"
                            + " guesses you would like to receive.");
            int max = scan.nextInt();
            int meGuess = scan.nextInt();
            // Round whatever random number is generated
            double randomNumber =
                    (int) Math.round((max * Math.random()) + 1);
            randomNumber = (int) randomNumber;
            System.out.println("Alright now guess which number I picked.");
            int numGuess = 0;
            while (numGuess <= meGuess)
                {
                    int guess = scan.nextInt();
                    if (guess < randomNumber)
                        {
                            System.out.println("Too low, my friend.");
                            numGuess++;
                        } else if (guess == randomNumber)
                        {
                            numGuess++;
                            System.out.print(
                                    "Wowee Zowee! You got it right! AI "
                                            + "will absolutely never sufficiently replace "
                                            + "human intellect.\n"
                                            + "It only took you "
                                            + numGuess);
                            if (numGuess == 1)
                                {
                                    System.out.println(" try.");
                                } else
                                {
                                    System.out.println(" tries.");
                                }
                            System.out.println("Do you want to play again? Y/N");
                            String answer = outScan.nextLine();
                            input = answer.equalsIgnoreCase("y");
                            numGuess = 0;
                            meGuess = 0;
                            max = 0;
                        } else if (guess > randomNumber)
                        {
                            System.out.println("Too high, buddy.");
                            numGuess++;
                        }
                    if (numGuess > meGuess)
                        {
                            System.out
                                    .println("Sorry buddy, you used up more"
                                            + " guesses than you told me to give you."
                                            + " Maybe next time.");
                            System.out.println("Do you want to play again? Y/N");
                            String answer = outScan.nextLine();
                            input = answer.equalsIgnoreCase("y");
                            numGuess = 0;
                            meGuess = 0;
                            max = 0;
                        }

                }
            scan.close();
            outScan.close();
        }}



}

Some background

  1. I am able to get this game to play at least once through with no problem. For some reason, sometimes the correct answer for the number to be guessed is +1 out of range (for example, I do max 3, guesses 5, and the correct answer ends up being 4 somehow). I am also allowed more guesses than I said I wanted. This is not my priority and I will fix that later, but I would like help with that. I'm sure I'm just overlooking something.

    1. Lastly, this is only one way I have tried to do this, by putting a while loop at the top of the main function. I have also tried to put the bulk of the code into a different function, as per the recommendation of another poster, and use a do-while loop, but that was also to no avail. Maybe I did not understand the specificities of how to do something like that, but I would get a similar, if not the same error.

I have put a bit of effort into resolving this problem myself and I hope this question meets community standards. I will be finishing this up myself later today so either way I'll be working on it but I would appreciate any help I could get. Thank you.

CelineDion
  • 638
  • 4
  • 14
  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo). This is likely causing or contributing to your problem. – Hovercraft Full Of Eels Jun 17 '18 at 17:35
  • Any place you have `int max = scan.nextInt();` any place you call `scan.nextInt()` follow it *immediately* with a call to `scan.nextLine();` to handle the end-of-line token. – Hovercraft Full Of Eels Jun 17 '18 at 17:36
  • **AND** you're closing your Scanner ***within*** the while loop! Don't do this! – Hovercraft Full Of Eels Jun 17 '18 at 17:41

1 Answers1

1

Several problems:

  • You're not handling the end of line token properly when getting ints from the Scanner. Follow any call to scan.nextInt() with scan.nextLine(); to handle and swallow this token
  • Your closing your Scanner within your while loop, so it is dead when the while loop repeats and no input is obtainable. Put that code to close outside the while loop.
  • You also need to reset your counter
  • You should only use one Scanner based on System.in
  • You're asking if you want to play again within the inner while loop which makes no sense. You need ask and get this information from within the outer while loop

  • Most important is that you need to learn how to debug. Please check out this great reference: How to debug small programs

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346