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");
}
}
I need a hand with this coding (I'm a beginner)
Page 1 of 15 Replies - 126 Views - Last Post: 02 March 2013 - 06:23 AM
#1
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?
Replies To: I need a hand with this coding (I'm a beginner)
#2
Re: I need a hand with this coding (I'm a beginner)
Posted 01 March 2013 - 06:15 PM
You can simplify:
Read very carefully the api docs for the calls you make to Random, as you have problems there
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
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.
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
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
(Math.random() returns a decimal between 0 and 1)
System.out.println(Math.random() * 100);
(Math.random() returns a decimal between 0 and 1)
#5
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 ...
#6
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:
Futher, use methods. e.g.
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
|
|

New Topic/Question
Reply



MultiQuote




|