# Fixing of bug in number guessing game

• April 15th, 2009, 04:22 PM
big_c
Fixing of bug in number guessing game
hey everyone i am stuck with this program. im trying to make a number guessing game using loops and if statements. i know how to do them individually but i am struggling with doing them together. heres what i have so far and useful feedback is greatly appreciated. oh yeah its all supposed to be in the main.

Code :

```import java. util .Scanner; import java.util.*;   public class NumberGame { public static void main(String[] args) { Random random = new Random(); int rand = random.nextInt(1000);   for(rand=1000; rand<=1000; rand++) { System.out.println("please enter a number between 1 and 1000"); if(guess<=500);   }   private int guess; } }```

im not sure if i have the for loop set up right but the part thats got me all confused is the if statement.
• April 15th, 2009, 04:26 PM
JavaPF
Re: help with this program (loops and if statements togethor)
What input/output are you looking for? Please give me an example.
• April 15th, 2009, 04:29 PM
big_c
Re: help with this program (loops and if statements togethor)
like if the random number is 559 and i put in 340 it should say something like your guess is to low try guessing between 341 and 560.
• April 15th, 2009, 04:40 PM
big_c
Re: help with this program (loops and if statements togethor)
i think i figured one thing out i have to do a nested loop with in order to have guess loop until the answer is right.
• April 15th, 2009, 04:45 PM
JavaPF
Re: help with this program (loops and if statements togethor)

Code :

```import java.util.Scanner; import java.util.*;   public class NumberGame {   public static void main(String[] args) {   Scanner sc = new Scanner(System.in);   Random random = new Random(); int rand = random.nextInt(100);   int guesses = 0;   System.out.println(rand);   System.out .println("I'm thinking of a number between 0 and 100. Whats your guess?");   while (sc.hasNextInt()) { int myGuess = sc.nextInt(); if (myGuess == rand) { guesses++; System.out.println("Congratulations! I was thinking " + rand + ". It took you " + guesses + " guesses."); break; } else if (myGuess < rand) { System.out.println("You need to guess higher"); guesses++; } else if (myGuess > rand) { System.out.println("You need to guess lower"); guesses++; } }   }   }```
• April 15th, 2009, 05:41 PM
big_c
Re: help with this program (loops and if statements togethor)
sweet its just what i was looking for. im really good with the while loop idk why i just didnt stick with that in the first place
• April 16th, 2009, 02:15 AM
JavaPF
Re: help with this program (loops and if statements togethor)
Quote:

Originally Posted by big_c
sweet its just what i was looking for. im really good with the while loop idk why i just didnt stick with that in the first place

Glad I could help.

Its all trial and error when you first start programming. You need to try different methods to find out what works and what doesn't. Soon you will know the best route to take :)