java number game fixed

Turns out I fixed my problem by putting the game loop into it's own method.  That allowed me to run the code involved in the game only if the player says they want to play.  I tried moving the
variables all over the place to get them to work how I wanted them too, but in the end it was best to put all the stuff I needed into a separate method, and then have the other method run it if the player says yes.  Putting the variables into the while loop would mess it all up.  I tried doing all kinds of stuff until I decided to put it into it's own method.  If there's a better option, I'll update.

Remember I'm new at this,  so I wouldn't copy any of this for an actual college class, I have no idea if it's good coding.  I just like to make stuff run at the moment, and will worry about the flowery bits later.

You do not have to know how to read to speak,  you do not have to know the rules of grammar to communicate.  No one will ever know if you spell a word wrong in your head if the word is properly used and enunciated.

I'll work on making it clean and pretty after I learn to speak.

package nellie.tobey.guessmynumber;
import nellie.tobey.mywindow.MyWindow;
import java.util.Random;
public class GuessMyNumber extends MyWindow {
 public GuessMyNumber() {
  String s = input();
 if (s.startsWith("y") || s.startsWith("Y")) {


 public void game() {
  int guessCount = 1;
  Random rand = new Random();
  int myGuess = promptForInt("Pick a number between 0 - 100");
     int myNumber = rand.nextInt(101);
     String s = "y";

   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?");
        if (s.startsWith("y") || s.startsWith("Y")) {
         myGuess = promptForInt("Pick a number between 0 - 100");
         myNumber = rand.nextInt(101);
         guessCount = 1;}


 /*   these are the bits I got out of my MyWindow to use in the game.
  * protected promptForInt(String prompt) {
  int i = 0;
  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;}
 //protected String promptForString(String prompt) {
 //String s = input();
 //return s;}

 public static void main(String[] args) {
  new GuessMyNumber();


Popular posts from this blog

Statistics, Python, Making a Stem Plot

Pandas Python and a little probability

JavaScript Ascii animation with while loops and console.log