Results 1 to 7 of 7
- 03-25-2009, 01:35 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 4
- Rep Power
- 0
Help about Guess the Numbers Program in java
Hey guys,
I'm new here. Hello. I have a question for you huys. I've been stuck here for more than 2 hours figuring this out.
Problem
Create a menu-driven "Guess the Number Game" that allows the player to choose from two options:
Version 1 - allows unlimited guesses
Version 2 - allows 5 guesses
Specifications
Create a GuessTheNumber class with the following members
Instance variables
keyBd : Scanner
myGuess, numberToGuess, count : Integer
Methods
GuessTheNumber( guess : Integer )
hint: initialize numberToGuess to guess, count to 0, create keyboard Scanner
showInstructions()
hint: displays "how to play the game" instructions for each version
version1()
hint: Guess3.java attached
version2()
hint: Guess4.java attached
Create a MenuGuess class that uses a menu to test the GuessTheNumber class
The menu will have the fallowing options
Instructions
Version 1
Version 2
Exit
Can anyone help me out here? I am totally confused and desperate. It is not on my textbook and I don't know anywhere else to search on.
Thank you very much.
- 03-25-2009, 03:31 AM #2
Member
- Join Date
- Mar 2009
- Posts
- 4
- Rep Power
- 0
Anyone knows this? I'm stuck here. Thank you.
-
What's specifically your question?
- 03-25-2009, 03:48 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 4
- Rep Power
- 0
Create a menu-driven "Guess the Number Game" that allows the player to choose from two options:
Version 1 - allows unlimited guesses
Version 2 - allows 5 guesses
How to combine the 2 versions and make the GuessTheNumber class? What command did I miss?
-
Since both versions repeat, this should tell you that you will need loops. In one version, you know the maximum number of times the loop will occur, in the other you don't. That suggests to me that one will use a for loop that has a break statement in it if the game is solved, and the other will use a while loop. Do you know which will use which? Also, it may be easier to put these versions each in their own method and just have your menu call the appropriate method based on the user's selection.
- 03-25-2009, 03:57 AM #6
Member
- Join Date
- Mar 2009
- Posts
- 4
- Rep Power
- 0
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
public static void main(String[] args) {
//declare variables
Scanner keyBd = new Scanner( System.in );
int myGuess, //player guess
numberToGuess, //random number to guess
count; //guesses used
//show introductory message
System.out.println("\nWelcome to Guess-My-Number\nA GAME of CHANCE and SKILL!");
System.out.println("You have 5 guesses - Good luck!");
//pick a random number between 1 and 25
//Random rNumGen = new Random();
//numberToGuess = 1 + rNumGen.nextInt(25);
numberToGuess = 13;
//give player only 5 tries to guess
for(count=1; count<=5; count++)
{
//get a number from the user
System.out.print("\nEnter a number between 1 and 25: ");
myGuess = keyBd.nextInt();
//good or bad guess??
if(myGuess < numberToGuess)
//guess too low
System.out.printf("\nYour guess [ %d ] was too low...\n", myGuess);
else if(myGuess > numberToGuess)
//guess too high
System.out.printf("\nYour guess [ %d ] was too high...\n", myGuess);
else
{
//good guess
System.out.printf("\nCongratulations!\nYou guessed [ %d ] in %d %s",
numberToGuess, count, (count>1)? "tries." : "try.");
break; //out of for
}//end else
System.out.printf("%s", count<5?"Try Again!":"\nSorry, Game Over!");
}//end for
if(count > 5)
{
//exceeded guesses allowed
System.out.printf("\nYou had your %d tries.\n", count-1);
}//end if
System.out.println();
}//end main()
}//end GuessTheNumber
This is my code. I don't know where to put the loop. Can you give me an idea/ Thank you so much for your reply.
-
1) plan the logic of the program on paper before trying to commit it to code. If you do it on paper, you will see for yourself where the loops should go.
2) Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Similar Threads
-
prime numbers program
By i contra i in forum New To JavaReplies: 9Last Post: 01-15-2009, 07:22 AM -
guess number game
By mistah in forum New To JavaReplies: 10Last Post: 11-23-2008, 03:37 AM -
sample of guess high and low game
By pouria62 in forum AWT / SwingReplies: 1Last Post: 10-26-2008, 12:57 PM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Addition program that displays the sum of two numbers
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:46 PM


LinkBack URL
About LinkBacks

Bookmarks