Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » While loop(my game loop)
While loop [message #774351] Tue, 03 January 2012 18:57 Go to next message
zach kalb is currently offline zach kalbFriend
Messages: 5
Registered: January 2012
Junior Member
Hi, I made a simple number guessing game that works but the program closes after each guess. so I tried a while loop and this is what i coded.


import java.util.Scanner;


public class Guessgame {

/**
* @param args
*/
public static void main(String[] args) {
while (num != 23){

System.out.println("Welcome To The Guessing Game!");
System.out.println("Please enter a guess");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
if (num > 23){
System.out.println("That Number Is To High");
}
else if (num < 23){
System.out.println("That Number Is To Low");
}
else {
System.out.println("You Guessed The Right Number");
}
}
System.out.println("Created By: Zach");
}

}

I know it has something todo with the num hasent been declared as an int before hand.
But i cant figure out how to make it work.

any suggestions?

Thanks zach

[Updated on: Tue, 03 January 2012 18:58]

Report message to a moderator

Re: While loop [message #774406 is a reply to message #774351] Tue, 03 January 2012 21:20 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

There is no while loop in the code appended. The code as appended is a
one-shot piece of code. Runs once and terminates.

--
Thanks,
Rich Kulp
Re: While loop [message #774519 is a reply to message #774406] Wed, 04 January 2012 04:32 Go to previous messageGo to next message
zach kalb is currently offline zach kalbFriend
Messages: 5
Registered: January 2012
Junior Member
So is there a way to get it to repeat until the user guesses the right anwser?

Thanks,
Zach
Re: While loop [message #774530 is a reply to message #774519] Wed, 04 January 2012 05:09 Go to previous messageGo to next message
zach kalb is currently offline zach kalbFriend
Messages: 5
Registered: January 2012
Junior Member
Never mind I just used a method and used it in the code. here is an example of my login program you can try if you want



import java.util.Scanner;


public class Login {

public static void main(String[] args) {
System.out.println("Welcome to the login system. ");
first();

}
public static void startgame(){
System.out.println("Welcome To The Guessing Game!");
System.out.println("Please enter a guess");
game();
}
public static void game(){
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
if (num > 23){
System.out.println("That Number Is To High");
System.out.println("Please Try Again");
game();
}
else if (num < 23){
System.out.println("That Number Is To Low");
System.out.println("Please Try Again");
game();
}
else {
System.out.println("You Guessed The Right Number");
System.out.println("Please Try Again");
game();
}


System.out.println("Created By: Zachary Kalb Copy Right 2012-2016");
System.out.println("Would you like to go back to the menu? 1 for yes and 2 for no");
Scanner input1 = new Scanner(System.in);
int w = input1.nextInt();
if (w == 1){
login();
}
}
public static void first(){
System.out.println("Please Enter The Password: ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
if (x == 24194995){
System.out.println("Login Successful!");
login();
}
else {
System.out.println("Incorrect Please Try Again");
first();
}
}
public static void login(){
System.out.println("1.The One Try Guess Game");
System.out.println("2.View The Secret Anwser");
System.out.println("3.Logout");
System.out.println("4.Exit");
Scanner input = new Scanner(System.in);
int y = input.nextInt();
if (y == 1){
startgame();

}
if (y == 2){
System.out.println("Zach Of Supertzag. www.youtube.com/supertechzandgames");
System.out.println("Would You Like To Go Back To The Menu? 1 for yes and 2 for no");
int a = input.nextInt();
if (a == 1){
login();
}
else{

}
}
if (y == 3){
System.out.println("You Have Been Loged Out");
first();
}
if (y == 4){
System.out.println("Thanks You For Using The Zach Login System");
}
}
}


Thanks for the help
Zach
Re: While loop [message #779405 is a reply to message #774530] Sun, 15 January 2012 16:45 Go to previous message
Tim Bennett is currently offline Tim BennettFriend
Messages: 10
Registered: January 2012
Junior Member
Zach,

Keep in mind that 90% of programming happens BEFORE you ever touch the computer. Programming is simply a set of instructions. You can write the instructions down on a sheet of paper, and ask somebody to follow them. That is still programming.

In order to become a better programmer, you need to ask yourself questions when you are developing your program, then answer those questions.

For example, a while loop will only check the condition when the loop is entered (I'm not sure how you were able to compile and run that program, as you are asking the while loop to make a comparison against an object that has not been created and instantiated yet.(num))

Here is a copy of the code you posted:

import java.util.Scanner;

public class Guessgame {

	/**
	* @param args
	*/
	public static void main(String[] args) {
		while (num != 23){

			System.out.println("Welcome To The Guessing Game!");
			System.out.println("Please enter a guess");
			Scanner scan = new Scanner(System.in);
			int num = scan.nextInt();
			if (num > 23){
				System.out.println("That Number Is To High");
			}
			else if (num < 23){
				System.out.println("That Number Is To Low");
			}
			else {
				System.out.println("You Guessed The Right Number");
			}
		}
		System.out.println("Created By: Zach");
	}
}


The first thing you need to do is get a number from the command line (otherwise, what are you going to compare?).

So, start off by asking for a number. THEN you can start your while loop. So we first move the first four lines inside the while loop, outside. That way, we now have a number to compare for our while loop.

import java.util.Scanner;

public class Guessgame {

	/**
	* @param args
	*/
	public static void main(String[] args) {

		System.out.println("Welcome To The Guessing Game!");
		System.out.println("Please enter a guess");
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();

		while (num != 23){

			if (num > 23){
				System.out.println("That Number Is To High");
			}
			else if (num < 23){
				System.out.println("That Number Is To Low");
			}
			else {
				System.out.println("You Guessed The Right Number");
			}
		}
		System.out.println("Created By: Zach");
	}
}


However, this presents another problem. What happens if our user enters the number '23' on the first try? The program will simply exit after displaying "Created By: Zach", because the while loop is never entered. To fix this, you should bring the "You Guessed The Right Number" statement outside the while loop. That way, when the while loop exits (which it will only do if the correct number is entered), the "You Guessed The Right Number" statement is still displayed, even if they get it right on the first guess.

import java.util.Scanner;

public class Guessgame {

	/**
	* @param args
	*/
	public static void main(String[] args) {

		System.out.println("Welcome To The Guessing Game!");
		System.out.println("Please enter a guess");
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();

		while (num != 23){

			if (num > 23){
				System.out.println("That Number Is To High");
			}
			else if (num < 23){
				System.out.println("That Number Is To Low");
			}
		}
		System.out.println("You Guessed The Right Number");
		System.out.println("Created By: Zach");
	}
}


This brings us to another problem. The number is never updated after the while loop is entered. This makes it an infinite loop if a number other than 23 is entered, because it's going to keep comparing the same number over and over again. In order to fix this, we can instantiate 'num' to '-1', and then ask for the input from the command line. The user won't know that we've already entered the while loop. It will be transparent to the user. This way, the while loop is guaranteed to be executed at least once. Another way to guarantee execution of a while loop at lease once is to use a do - while loop. You can look that up on your own if you feel so inclined.

Also, if you're going to repeat the while loop until they guess the correct number, it's a good idea to inform the user they are expected to enter another number. Take a look at the while loop now:

import java.util.Scanner;

public class Guessgame {

	/**
	* @param args
	*/
	public static void main(String[] args) {

		System.out.println("Welcome To The Guessing Game!");
		System.out.println("Please enter a guess");
		Scanner scan = new Scanner(System.in);
		int num = -1;

		while (num != 23){

			num = scan.nextInt();

			if (num > 23){
				System.out.print("That number is too high. Please enter another number: ");
			}
			else if (num < 23){
				System.out.print("That number is too low. Please enter another number:  ");
			}
		}
		System.out.println("You Guessed The Right Number");
		System.out.println("Created By: Zach");
	}
}


Now, when the while loop calls for input, the user will have been notified that their last response was incorrect, and they are expected to enter another number.

The difference between 'System.out.println' and 'System.out.print' is that the println statement will automatically add a carriage-return and linefeed to the output. This means that when the user enters a number, it will be on a new line. Your next output will be on a new line as well. By using the 'print' statement instead of the 'println' statement, you keep the cursor at the end of the line of text you just typed. That way, when the user goes to enter the number, it will be on the same line as your output. This could be a desired feature, or you could want every input on a new line. It's totally up to you, the programmer, to make that decision. There are often more ways than one to do something in programming.

Finally, here is your program with a few modifications to enable a random number to be generated between 0 and 999. When you run it, it will always generate a different number. Feel free to use this code as your own.

Good luck in your programming journey.

import java.util.Random;
import java.util.Scanner;

public class GuessGame {

	/**
	* @param args
	*/
	public static void main(String[] args) {

		System.out.println("Welcome to the Guessing Game!");
		System.out.print("Please enter a number: ");
		Scanner scan = new Scanner(System.in);
		int num = -1;
		Random randomNumber = new Random();
		int targetNum = randomNumber.nextInt(1000);

		while (num != targetNum){

			num = scan.nextInt();

			if (num > targetNum){
				System.out.print("That number is too high. Please enter another number: ");
			}
			else if (num < targetNum){
				System.out.print("That number is too low.  Please enter another number: ");
			}
			else {
				System.out.println("You guessed the right number!");
			}
		}
	}
}
Previous Topic:CodePro Analytix Results Inconsistency
Next Topic:Thread active only when debuggin in JDT
Goto Forum:
  


Current Time: Fri Mar 29 05:50:57 GMT 2024

Powered by FUDForum. Page generated in 0.03417 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top