# I need a hand with this coding (I'm a beginner)

Page 1 of 1

## 5 Replies - 609 Views - Last Post: 02 March 2013 - 06:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313998&amp;s=4c4801a8ab4edab37473611ebb6e2861&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Pmike86

Reputation: 0
• Posts: 11
• Joined: 01-March 13

# I need a hand with this coding (I'm a beginner)

Posted 01 March 2013 - 05:50 PM

I have a project that I am working on. It is a game where the computer generates a random number between 1-100 and you get 6 oppurtunities to guess the correct answer. Everything seems to run just fine. My only issue now is that the program is not displaying the correct if statements. I do not know if my if else statements are wrong or if the computer is choosing a new random number each time. I tried to test this new random number generation by printing and displaying "r" which you'll see in the code. Can anyone point me into the proper direction or give me any help. I think I am 95% done this thing after this. Thanks alot! (btw I have my print out of "r" as a comment right now) - for future reference how could i test this properly?

```
import java.util.*;

public class project2 {
public static void main(String[] args){
Scanner in = new Scanner (System.in);
System.out.println("Welcome, I am thinking of a number between 1-100. Take a guess!");
Random r = new Random(101);
int actual = r.nextInt();
//System.out.print(r);
int totalcount = 6;
int gamecounter = 0;
while ( gamecounter != totalcount) {
int counter = 0;
int turncount = 1;
int guess = in.nextInt();
while (counter != turncount) {
if ( guess < actual) {
System.out.println("You guessed " + guess);
System.out.println("Too Low");}
if ( guess > actual){
System.out.println("You guessed " + guess);
System.out.println("Too High");}
else {
System.out.println("You guessed " + guess);
System.out.println("YOU'RE PSYCHIC!");}
counter++;
}
gamecounter++;
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: I need a hand with this coding (I'm a beginner)

### #2 g00se

• D.I.C Lover

Reputation: 3214
• Posts: 14,150
• Joined: 20-September 08

## Re: I need a hand with this coding (I'm a beginner)

Posted 01 March 2013 - 06:15 PM

You can simplify:

```        int totalcount = 6;
int gamecounter = 0;
int guess = -1;

while ((guess != actual) && (gamecounter < totalcount)) {
guess = in.nextInt();

if (guess < actual) {
System.out.println("You guessed " + guess);
System.out.println("Too Low");
} else if (guess > actual) {
System.out.println("You guessed " + guess);
System.out.println("Too High");
} else {
System.out.println("You guessed " + guess);
System.out.println("YOU'RE PSYCHIC!");
}

gamecounter++;
}

```

Read very carefully the api docs for the calls you make to Random, as you have problems there

### #3 flareback

Reputation: 3
• Posts: 30
• Joined: 27-February 13

## Re: I need a hand with this coding (I'm a beginner)

Posted 01 March 2013 - 09:41 PM

I agree with g00se, you need to look at the Random api.

For testing, print statements will be your friend on this one. I'd suggest printing the value of 'r' each time in testing. Also, for a simple program like this just play through the game several times.

### #4 CarDriver

• D.I.C Regular

Reputation: 65
• Posts: 431
• Joined: 17-February 11

## Re: I need a hand with this coding (I'm a beginner)

Posted 01 March 2013 - 09:54 PM

You can generate a random number between 0 and 100 with
```System.out.println(Math.random() * 100);
```

(Math.random() returns a decimal between 0 and 1)

### #5 g00se

• D.I.C Lover

Reputation: 3214
• Posts: 14,150
• Joined: 20-September 08

## Re: I need a hand with this coding (I'm a beginner)

Posted 02 March 2013 - 04:14 AM

Quote

You can generate a random number between 0 and 100 with ...
NO - you can't

### #6 baavgai

• Dreaming Coder

Reputation: 6578
• Posts: 13,908
• Joined: 16-October 07

## Re: I need a hand with this coding (I'm a beginner)

Posted 02 March 2013 - 06:23 AM

Ooo... can I simplify? I love this game:
```int maxGuess = 6;
bool done = false;
while (!done) {
int guess = in.nextInt();
System.out.println("You guessed " + guess); // because, seriously, it shows up in all cases
int cmp = guess - actual;
if (cmp==0) {
System.out.println("YOU'RE PSYCHIC!");
done = true;
} else {
System.out.println("Too " + (cmp<0?"Low":"High");
done = (--maxGuess<1);
}
}

```

Futher, use methods. e.g.
```// returns if won
bool play(int actual, int maxGuess) { /* your code here */ }
//...

if(play(actual, 6)) {
System.out.println("YOU'RE PSYCHIC!");
} else {
System.out.println("Sorry, too many guesses.");
System.out.println("The answer was " + acutal);
}

```

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}