New assignment new problem...
Hey,
Well I passed my last assignment that I had help from here on with a fairly high grade. Now that I've run into another little problem I decided to come back and get some help again. =) The problem this time is that when I run my program in the console it freezes after Player 2 enters a number; I know this has something to do with my 'while' loop but I can't seem to see any problem with the way it's set up.
Code :
import java.io.*;
class Assignment7Question1
{
public static void main (String[] args) throws IOException
{
int intFirstInt = 0, intGuesses = 0, intGuessedNumber = 0; //Creates the three variables that will be used in this program.
InputStreamReader inStream = new InputStreamReader (System.in);
BufferedReader userInput = new BufferedReader (inStream);
String inputData; //Creates one string variable
System.out.println ("Player 1:"); //Prints the sentence to the cmd.
System.out.println ("Please input a positive number that is less than 1000:"); //Prints the sentence to the cmd.
inputData = userInput.readLine ( ); //Places input into inputData string variable.
intFirstInt = Integer.parseInt(inputData); //Sets the value saved into inputData to the intFirstInt variable.
System.out.println ("Player 2:"); //Prints the sentence to the cmd.
System.out.println ("Please enter the number that you believe Player 1 has entered."); //Prints the sentence to the cmd.
inputData = userInput.readLine(); //Places input into inputData string variable.
intGuessedNumber = Integer.parseInt(inputData); //Sets the value saved into inputData to the intGuessedNumber variable.
while (intGuessedNumber > intFirstInt | intGuessedNumber < intFirstInt);
{
intGuesses = (intGuesses + 1); //Counts the number of guesses that Player 2 takes to get the correct number.
if (intGuessedNumber > intFirstInt)
System.out.println ("Your guess is too low! Please try again."); //Prints the sentence to the cmd.
else if (intGuessedNumber < intFirstInt)
{
System.out.println ("Your guess is too high! Please try again."); //Prints the sentence to the cmd.
}
inputData = userInput.readLine(); //Places input into inputData string variable.
intGuessedNumber = Integer.parseInt(inputData); //Sets the value saved into inputData to the intGuessedNumber variable.
}
if (intGuessedNumber == intFirstInt)
{
System.out.println ("Congratulations, you guessed correctly!");
System.out.println ("It took you "+intGuesses+" guesses to determine the number.");
}
}
}
Sorry for some of the indentation being off, when I CnP the code from Notepad++ to here it messes it up.
Re: New assignment new problem...
Hi tyeeeee1! First of all, I'd like to mention that I'm still a beginner in java myself, so I'll apologize ahead of time if I give you any false information. Anyways, on to your problem. I'd like to recommend using print statements to try and debug this. I quickly went over your code, which you could edit and fix the indents, and I understand what you're trying to do. I'd like you to look at your variables and see what inputs they contain: for example, you're Player 1 and 2 name inputs. *hint* Try printing out inputData. Also, really understand your while loop and your if statements. What exactly are you asking your loop and statements to do? Think a bit about the placement of them as well. If you're still confused, try going through your code with pen(cil) and paper and do exactly what the code you're writing does, kind of like you did with the comments.
Re: New assignment new problem...
while (intGuessedNumber > intFirstInt | intGuessedNumber < intFirstInt); << Remove colon.
| is not the correct operator. You're looking for ||.
See: Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: New assignment new problem...
Quote:
Originally Posted by
newbie
Newbie has hooked you up, just a small syntax error! I will say this looking through your code though. I have a feeling people are going to be really confused when they guess to high, but the program outputs they guessed to low! ;)
Re: New assignment new problem...
I changed the || to a | while I was trying to fix it up and then forgot to change it back. The error was the ; I had at the end of the while statement, after that I noticed the error that Chico pointed out and fixed it up. Thanks for all the help guys and gals!
Re: New assignment new problem...
IF you don't mind, do you think you can write assignment and post it. I would like to take a crack at it. THanks.