Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java program help

Status
Not open for further replies.

Fromnowon

Technical User
Dec 2, 2005
3
GB
im trying to get a program to run the way i want it to. It compiles fine but it isnt performing things correctly. The program is a number generator game which the computer generates and then the user has to guess. In order for the user to guess the number there needs to be some intructions i.e. "your guess needs to be higher/lower".
These instructions are not outputted to the user therefore it is virtually impossible for the user to find this number.
The value is 1 to 1000 and anything outside this boundary displays the higher/lower message

I think it may be something to do with the while loop "
Code:
} while ((user_number > 0) && (user_number <= 1000));
The full code is
Code:
import java.util.*;

public class game
{
	public static void main(String[] args)
	{
		// Declare variables, setup keyboard input and the 
		// random number generator
		int game_number, user_number;
		String continue_pref; //; not previously there
		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 > 0) && (user_number <= 1000)); 
			
			// 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("You need to guess higher%n");
				else
					System.out.printf("You 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 > 0) && (user_number <= 1000));
			}
			
			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: or (n/N) No: ");
				continue_pref = new String(data_input.next());
			}
			else
			{
				// User wants to quit
				continue_pref = new String("No");
			}
		} while (continue_pref.equalsIgnoreCase("Y"));
	}
}

any form of feedback would be appreciated
 
I think it may be something to do with the while loop
nice joke!
You have 700 while-loops (and they don't make much sense).

At multiple places you ask for a number, and even more often you prove it being in the right scope, and then: you repeat.

Shouldn't you repeat the question if the number is out of scope?

... while (n < 1 || n > 1000);

seeking a job as java-programmer in Berlin:
 
sorry, i meant to say this while loop not the whole thing.
I tried that but this is my outcome:

untitled6yl.jpg


Good thing is the instructions are now working but the number doesnt seem to be generating (according to this the right number should be 163) + 0 isn't quitting the program. Any ideas ;) ?
 
You just need a while loop, no that bunch.

I'd get a debugger, then you will clearly see what's really happening: you've coded a pinball. Once the program gets out of a loop, it just enters the next instead of quitting the program.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top