5 Replies - 347 Views - Last Post: 02 March 2013 - 06:23 AM Rate Topic: -----

#1 Pmike86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!");
  System.out.print("Please enter your number: ");
  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++;                 
  }
    System.out.println("Game Over Please Try Again");
}
}




Is This A Good Question/Topic? 0
  • +

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

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,302
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 flareback  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,302
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • 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);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1