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
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.
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.
Several problems:
scan.nextInt()
with scan.nextLine();
to handle and swallow this tokenSystem.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