Results 1 to 2 of 2
Thread: guessing game
- 02-02-2011, 07:49 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
guessing game
can anyone help me with these questions?
import java.util.Scanner;
import java.util.Random;
public class Guess {
static int lowNumber=1;
static int highNumber=100;
static int numOfGuessesAllowed=10;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random generator = new Random();
int target = generator.nextInt(highNumber) + lowNumber;
System.out.println(target);
int numOfGuesses=0;
while(numOfGuesses < numOfGuessesAllowed) {
//Some of these lines should go into the getNumberFromUser() method
System.out.println("Guess a number:");
int guess = sc.nextInt();
boolean hasWon = checkNumber(guess, target);
numOfGuesses++;
if(hasWon) {
System.out.println("You win");
System.exit(0);
}
}
System.out.println("You lose");
System.exit(0);
}
public static boolean checkNumber(int guess, int target) {
if(guess < target) {
System.out.println("Higher");
} else if (guess > target) {
System.out.println("Lower");
}
return guess==target;
}
//Fill in this method with code to parse input from the user and
//Ensure it is a number between low and high values.
public static int getNumberFromUser(Scanner sc) {
}
}
//Modify the guessing game that allows a user to guess a number. The program should output "higher" or "lower" depending on each guess.
1. Document the code to explain what is going on. The useful links section below might provide some useful resources to explain any classes/objects you may be unfamiliar with.
Try the guessing game. How well does it work? Explain what happens when you enter the following values as a guess:
13
a
4.0
4.5
3 (a bunch of spaces followed by a 3)
-4
- 4 (a negative sign, followed by a bunch of spaces, followed by a 4)
Explain why you think you get the results you do for each entry.
2. Modify the program to remove the "sudden stop" of the two calls to System.exit(0). You can use break or any other technique, but I want the code to leave the while loop as soon as the user wins. I do not want the program to output "You lose" when the user wins.
3. Move all Scanner input code to the method provided (it is calledgetNumberFromUser() ) and is currently empty of code. You need to add code that does the following:
a. Check that the input is a integer; and
b. Ensure that the integer is in the proper range of legal guesses.
4. Modify the code to add a menu at the beginning of the game. It should have 2 options:
1. Classic
2. Custom
Check if the user choses 1 or 2. If they chose 1, play the game with the default values (1-100, 10 guesses). If they chose 2, ask them to enter 3 values: A low value, a high value, and a number of guesses. For example, if the user enter 4, 10, and 9999, the game would generate a number between 4 and 10 and allow 9999 guesses before the player loses.
Bonus:
There is a lot of code you can add to ensure "tidy" inputs. For example, you should check that the value entered for High is legal compared to the value entered for Low. Thus, the custom games values of 100, 10, 99 is not legal, since you cannot have a number range of 100 to 10. There are many ways to deal with this issue, try and find one that works for you.
Submit the following by the deadline:
Your working program, properly documented as a .java file.
A copy of your plan, a written document explaining how you are going to implement your solution.
-
Thread closed as a homework dump. I'm sure that this wasn't your intention, and so I invite you to repost your question, but please demonstrate some effort in the post and ask specific questions that you are unclear of rather than just dumping your assignment in the forum. If you show some effort, you'll likely find many forum members who will be more than happy to help you.
Last edited by Fubarable; 02-02-2011 at 08:00 PM.
Similar Threads
-
Guessing Game with for loop
By ccart62 in forum New To JavaReplies: 3Last Post: 11-22-2010, 12:55 AM -
Need help in Guessing Game
By rose in forum Java GamingReplies: 4Last Post: 10-27-2010, 10:43 PM -
Guessing Game
By rose in forum Java GamingReplies: 4Last Post: 10-27-2010, 08:00 PM -
guessing game help
By yasmin k in forum AWT / SwingReplies: 4Last Post: 10-31-2009, 05:37 PM -
guessing game using GUI
By yasmin k in forum New To JavaReplies: 1Last Post: 10-26-2009, 12:13 PM


LinkBack URL
About LinkBacks

Bookmarks