AP Notes, Outlines, Study Guides, Vocabulary, Practice Exams and more!

need help....guessing game

6 posts / 0 new
Last post
et_thehuman's picture
Joined: Dec 2007
need help....guessing game

for this im supposed to create a program that randomly chooses a number between 1-100 for the user to guess and if the user is wrong then ask for another guess
then wen the user gets it right, to then state the amount of tries and to ask if the user wants to play again

heres the code that my friend and I have com up with...

import java.util.Scanner;
import java.util.Random;
import cs1.Keyboard;

public class Guessing
public static void main (String[] args)
final int max=10;
int answer=0;
int guess=0;
int reportCount=0;
int decsion;

Scanner scan = new Scanner (System.in);

Random generator = new Random();
answer = generator.nextInt(max) + 1;

System.out.println ("I'm thinking of a number between 1 and "+max+".");
System.out.print ("Guess what it is: ");
guess = scan.nextInt();
System.out.print ("Sorry wrong answer please guess again: ");
guess = scan.nextInt();
if (guess==answer){
System.out.println("You got it! Good Guessing!");
break; }
System.out.print ("");}


String decision,input;
decision=new String("again");
System.out.println("Type the word:'again' if you want to play another round. ");
input = Keyboard.readString();

System.out.println("I did not understand you please type again.");
input = Keyboard.readString();

wat i need help on is how to count the amount of tries of the user and how to make the code restart at the top for another game

Hunterbender's picture
Joined: Nov 2007

why is it so complex? O_o

use math.Random()*100 and store it into an int. Then, compare it with the guess. If wrong, then countwrong++. If right, then done

sephirothrr's picture
Joined: Jan 2008

Actually, you'll need 1 + 100 * Math.random() in order to go from 1 to 100.
As for the repetition, just include a question at the end, like "Do you want to play again?", and if the user types "Yes", repeat, or else close.

cawn009's picture
Joined: Sep 2008

yup his right, same here you can use 1 + 100 * Math.random() for randomizing your guessing game program.

[URL=http://www.studyinaustralia.com]mba education in australia[/URL]

musicguy5896's picture
Joined: Oct 2008

hey, there's a way to write this way more easily. You don't need the random or keyboard classes. Start a while loop while the guess doesn't equal the number. Number should equal (-1 + Math.random() * 101);
Then keep making if statements for if it is above the number or below so it will give the player hints. Lastly, report the number and how many guesses

orsonl081's picture
Joined: May 2011

Thanks a lot to introduce such a nice tricks. I like the program created by you.

Need Help?

We hope your visit has been a productive one. If you're having any problems, or would like to give some feedback, we'd love to hear from you.

For general help, questions, and suggestions, try our dedicated support forums.

If you need to contact the Course-Notes.Org web experience team, please use our contact form.

Need Notes?

While we strive to provide the most comprehensive notes for as many high school textbooks as possible, there are certainly going to be some that we miss. Drop us a note and let us know which textbooks you need. Be sure to include which edition of the textbook you are using! If we see enough demand, we'll do whatever we can to get those notes up on the site for you!