lundi 19 octobre 2015

guessing game-Issue in nested if statements within While loops

I'm having an issue with my program. I have the basics down for the guessing game, but I'm having an issue implementing different phrases based on the number of guesses. At the moment, when the program runs and you enter a number, it immediately moves on to the end of the game, and displays the answer, without allowing multiple guesses. Here is my code. I've edited it to be formatted a bit better since everyone complained about it.:

import java.util.*;

public class Hw_five {


    public static void main(String[] args) {

        int answer;
        int tries = 0;
        answer = (int) (Math.random() * 99 + 1);

        System.out.println("Welcome to the Guess the Number Game ");
        System.out.println("+++++++++++++++++++++++++++++++++++++ \n");
        System.out.println("I'm thinking of a number between 1-100 ");
        System.out.println("Try to guess it! ");

        Scanner sc = new Scanner(System.in);
        String choice = "y";

        while (choice.equalsIgnoreCase("y")) {

            int guess = getIntWithinRange(sc, "Please enter a number: ", 0, 100);

            if (guess != answer) {

              if (guess == answer) { System.out.println("Your guess is correct! Congratulations!"); 
              } 


              else if (guess < answer)
              { System.out.println("Your guess was too low. Try again. ");
              tries++;
              }

              else {System.out.println(
                      "Your guess was too high. Try again."); tries++; }

            }

              System.out.println("The number was " + answer + " !");
              System.out.println("You guessed it in " + tries + " tries");




            //ask if they want to continue
            System.out.println("\n Continue (y/n)? ");
            choice = sc.next();

            System.out.println();

        }

        //print out thank you message
        System.out.println("Thanks for playing! Try again later. ");
        sc.close();
    }

    public static int getInt(Scanner sc, String prompt) {
        int i = 0;
        boolean valid;
        valid = false;

        while (valid == false)
        {
            System.out.println(prompt);
            if (sc.hasNextInt()) {
                i = sc.nextInt();
                valid = true;
            } else {
                System.out.println("Please enter a number. ");
                sc.next();
            }

        }
        return i;

    }

    public static int getIntWithinRange(Scanner sc, String prompt, int min, int max) {
        int i = 0;
        boolean isValid = false;
        while (isValid == false) {
            i = getInt(sc, prompt);
            if (i <= min) {
                System.out.println("Error! Number must be greater than " + min + ".");
            } else if (i >= max) {
                System.out.println("Error! Number must be less than " + max + ".");
            } else {
                isValid = true;
            }
        }
        return i;
    }
    }




Aucun commentaire:

Enregistrer un commentaire