java number guessing game
Ok, I wanted to modify this from the lesson in the book. Took me a while to get it to run right. Little lesson I learned.... Maximize your code screen so that the little {} doesn't hide on you offscreen.
I added code so the game will ask if you want to play again, and as simple as that seems, it took me a while to make it all run right.
After reviewing the program again, I realized that if you tell it no, after play again, or anything other then Y, y, it will still prompt for your number guess before it shuts down. I tried putting the line
(myGuess = promptForInt("Pick a number between 0 - 100"); at the beginning of the while loop, but that messed up to. Will make another post when I figure out how to fix it.
package nellie.tobey.guessmynumber;
import nellie.tobey.mywindow.MyWindow;
import java.util.Random;
public class GuessMyNumber extends MyWindow {
public GuessMyNumber() {
print("Play?");
String s = input();
int guessCount = 1;
Random rand = new Random();
int myGuess = promptForInt("Pick a number between 0 - 100");
int myNumber = rand.nextInt(101);
while (s.startsWith("y") || s.startsWith("Y")) {
if((myGuess < myNumber) && (myGuess != myNumber)) {
myGuess = promptForInt("Your guess is low, try again:");
guessCount += 1;
}
else if ((myGuess > myNumber) && (myGuess != myNumber)) {
myGuess = promptForInt("Your guess is high, try again:");
guessCount += 1 ;
}
else {
print("You win");
print("number of guesses " + guessCount);
s = promptForString("Play again?");
myGuess = promptForInt("Pick a number between 0 - 100");
myNumber = rand.nextInt(101);
guessCount = 1;}
}}
/* this code was part of the 'My window' class.
* protected promptForInt(String prompt) {
int i = 0;
print(prompt);
String s = input();
try {
i = Integer.parseInt(s);
}
catch (NumberFormatException e) {
print(s + " is not a valid number. Try again.");
i = promptForInt(prompt);
}
return i;}
*/
//this code is part of the 'My Window' class.
//protected String promptForString(String prompt) {
//print(prompt);
//String s = input();
//return s;}
public static void main(String[] args) {
new GuessMyNumber();
}
}
I added code so the game will ask if you want to play again, and as simple as that seems, it took me a while to make it all run right.
After reviewing the program again, I realized that if you tell it no, after play again, or anything other then Y, y, it will still prompt for your number guess before it shuts down. I tried putting the line
(myGuess = promptForInt("Pick a number between 0 - 100"); at the beginning of the while loop, but that messed up to. Will make another post when I figure out how to fix it.
package nellie.tobey.guessmynumber;
import nellie.tobey.mywindow.MyWindow;
import java.util.Random;
public class GuessMyNumber extends MyWindow {
public GuessMyNumber() {
print("Play?");
String s = input();
int guessCount = 1;
Random rand = new Random();
int myGuess = promptForInt("Pick a number between 0 - 100");
int myNumber = rand.nextInt(101);
while (s.startsWith("y") || s.startsWith("Y")) {
if((myGuess < myNumber) && (myGuess != myNumber)) {
myGuess = promptForInt("Your guess is low, try again:");
guessCount += 1;
}
else if ((myGuess > myNumber) && (myGuess != myNumber)) {
myGuess = promptForInt("Your guess is high, try again:");
guessCount += 1 ;
}
else {
print("You win");
print("number of guesses " + guessCount);
s = promptForString("Play again?");
myGuess = promptForInt("Pick a number between 0 - 100");
myNumber = rand.nextInt(101);
guessCount = 1;}
}}
/* this code was part of the 'My window' class.
* protected promptForInt(String prompt) {
int i = 0;
print(prompt);
String s = input();
try {
i = Integer.parseInt(s);
}
catch (NumberFormatException e) {
print(s + " is not a valid number. Try again.");
i = promptForInt(prompt);
}
return i;}
*/
//this code is part of the 'My Window' class.
//protected String promptForString(String prompt) {
//print(prompt);
//String s = input();
//return s;}
public static void main(String[] args) {
new GuessMyNumber();
}
}
Comments
Post a Comment