...

View Full Version : Need help desperately deadlines 2marrow



huggies07
12-13-2007, 06:34 PM
I am currently stuck this is the first time i have ever used java an it is wrecking my brains i dont understand whats wrong with it. It all works rite up to the point were it wont congradulate the use for getting the number right could anyone please please tell me whats wrong. I dnt want the answer just need a step in the right direction

Here is the code

import java.util.*;

public class guessgame
{
public static void main(String[] args)
{
// Declare variables, setup keyboard input and the
// random number generator
int game_number, user_number;
String continue_pref = "";
Scanner data_input = new Scanner(System.in);
Random generate = new Random();

do
{
// Generate game number
game_number = generate.nextInt(999) + 1;
// The following line is a debug line, comment out
// for real game.
System.out.printf("Game number:%d%n", game_number);

// Get users first guess
System.out.print("The computer has generated a number.");
do
{
System.out.printf("%nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
user_number = data_input.nextInt();

} while ((user_number >= game_number) && (user_number <= game_number));

// While user has not guessed right and does not want to quit
while ((user_number == game_number) || (user_number != 0))
{
if (user_number < game_number)
System.out.printf("%nYou need to guess higher%n");
else
System.out.printf("%nYou need to guess lower%n");
// Get users next guess
do
{
System.out.printf("%nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
user_number = data_input.nextInt();

} while ((user_number >= game_number) && (user_number <= game_number));
}

{
if (user_number == game_number)
// User has guessed right
System.out.printf("%nYou guessed correctly, well done.%nDo you want to play again (y/Y)=Yes: ");
continue_pref = new String("Yes");//(data_input.next());
}

{
if (user_number == 0)
// User wants to quit
continue_pref = new String("No");
}
} while (continue_pref.equalsIgnoreCase("N"));
}
}

Gox
12-14-2007, 12:17 AM
I didn't run your code, but it looks like the logic of your while-loop is incorrect to me.


// While user has not guessed right and does not want to quit
while ((user_number == game_number) || (user_number != 0))
{
...

This reads "Do this set of code (get user guess) while the user has guessed the right number or the user doesn't want to quit". I think you meant this condition to be "while the user hasn't guessed the right number or the user doesn't want to quit". So, maybe you want the following instead?


// While user has not guessed right and does not want to quit
while ((user_number != game_number) || (user_number != 0))
{
...


See if that helps.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum