Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please help! High-Low guessing game

    I need to fix some parts of the program such as when it says "invalid choice" when it was answered correctly. Also, I need the program to ask run again or end the session, when it was done running. thank you!
    the program is attached.


    import java.util.Scanner;
    import java.util.Random;

    /**
    * Please help!
    *
    * @Gun Yoon
    * @Jan 26th 2011
    */
    public class Highlow
    {
    private static int min;
    private static int max;
    private static int actual;
    private static int guessed;
    private static int guessCount;

    public static void run()
    {
    guessCount = 0;
    min = 1;
    max = 100;
    Random gen = new Random();
    actual = gen.nextInt(100) + 1;
    Scanner keyboard = new Scanner(System.in);
    boolean done = false;
    while(!done)
    {
    boolean validInput = false;
    while(!validInput)
    {
    System.out.print("Please choose between " + min + " and " + max + ": ");
    try
    {
    String actual = keyboard.next();
    keyboard.nextLine();
    int choice = Integer.parseInt(actual);
    if(choice < min || choice > max)
    {
    throw new NumberFormatException();
    }
    else
    {
    guessed = choice;
    validInput = true;
    }
    }
    catch (NumberFormatException e)
    {
    System.out.println("Wrong! Please enter number between " + min + " and "+ max);
    }

    }
    guessCount++;
    if(guessed < actual)

    {
    System.out.println("It's too low!");
    min = Math.max(min, guessed);
    }
    else if(guessed > actual)
    {
    System.out.println("That's too high!");
    max = Math.min(max, guessed);
    }
    else if(guessed == actual)
    {
    System.out.println("Finally... Good job, it took you "+ guessCount + " tries");
    done = true;
    }
    }
    }
    }

    Tester

    import java.util.Scanner;
    /**
    * Please help!
    *
    * @Gun Yoon
    *
    */

    public class HighlowTester
    {

    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner(System.in);
    boolean done = false;
    boolean validAnswer = false;
    boolean loop = false;
    boolean begin = false;
    while(!done)
    {
    System.out.print("Mr. Thompson, let's play a game (Say yes or Y, no to quit) ");
    String selection = keyboard.next();
    keyboard.nextLine();
    if(selection.equalsIgnoreCase("yes") || selection.equalsIgnoreCase("y"))
    {
    begin = true;
    done = true;
    }
    if(selection.equalsIgnoreCase("no") || selection.equalsIgnoreCase("n"))
    {
    begin = false;
    done = true;
    System.out.println("Game over");
    }
    else
    {
    System.out.println("Invalid choice! Please choose yes or no.");
    }



    if(begin)
    {
    boolean validChoice = false;
    int choice = 0;
    while(!validChoice)
    {
    System.out.println("choose the game you would like to play?(Enter the number from 1 to 4)");
    System.out.println("1. Guessing Game");
    System.out.println("2. Minesweeper");
    System.out.println("3. Chess");
    System.out.println("4. poker");
    try
    {
    String input = keyboard.next();
    choice = Integer.parseInt(input);
    if(choice < 1 || choice > 4)
    throw new IllegalStateException();
    else if(choice != 1)
    throw new UnsupportedOperationException();
    else
    validChoice = true;
    }
    catch(NumberFormatException e)
    {
    System.out.println("Invalid choice! Please choose from 1 to 4.");
    }
    catch(IllegalStateException e)
    {
    System.out.println("Please choose one from 1 to 4.");
    }
    catch(UnsupportedOperationException e)
    {
    System.out.println("This game contains virus");
    }
    }

    if(choice == 1)
    {
    Highlow Game = new Highlow();
    Game.run();
    }
    }

    }


    }
    }
    Attached Files Attached Files

  • #2
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    please help!

    please help!

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,027
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    Quote Originally Posted by gungun View Post
    please help!
    Can't help very much.
    This is the javascipt forum
    and your code is in java.

    The only thing they have in common is the first four letters of their names.

    Try posting on the "JAVA" forum.

  • #4
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I've corrected two issues for you in the HighlowTester class. The other one looks OK to me. Here's the solution:

    package highlow;

    import java.util.Scanner;
    /**
    * Please help!
    *
    * @Gun Yoon
    *
    */

    public class HighlowTester
    {

    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner(System.in);
    boolean done = false;
    boolean validAnswer = false;
    boolean loop = true;
    boolean begin = false;
    while(loop)
    {
    System.out.print("Mr. Thompson, let's play a game (Say yes or Y, no to quit) ");
    String selection = keyboard.next();
    keyboard.nextLine();
    if(selection.equalsIgnoreCase("yes") || selection.equalsIgnoreCase("y"))
    {
    begin = true;
    done = true;
    }
    else if(selection.equalsIgnoreCase("no") || selection.equalsIgnoreCase("n"))
    {
    begin = false;
    done = true;
    loop = false;
    System.out.println("Game over");
    }
    else
    {
    System.out.println("Invalid choice! Please choose yes or no.");
    }



    if(begin)
    {
    boolean validChoice = false;
    int choice = 0;
    while(!validChoice)
    {
    System.out.println("choose the game you would like to play?(Enter the number from 1 to 4)");
    System.out.println("1. Guessing Game");
    System.out.println("2. Minesweeper");
    System.out.println("3. Chess");
    System.out.println("4. poker");
    try
    {
    String input = keyboard.next();
    choice = Integer.parseInt(input);
    if(choice < 1 || choice > 4)
    throw new IllegalStateException();
    else if(choice != 1)
    throw new UnsupportedOperationException();
    else
    validChoice = true;
    }
    catch(NumberFormatException e)
    {
    System.out.println("Invalid choice! Please choose from 1 to 4.");
    }
    catch(IllegalStateException e)
    {
    System.out.println("Please choose one from 1 to 4.");
    }
    catch(UnsupportedOperationException e)
    {
    System.out.println("This game contains virus");
    }
    }

    if(choice == 1)
    {
    Highlow Game = new Highlow();
    Game.run();
    }
    }

    }


    }
    }

  • #5
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks Distant job!

    Thanks you!

    I never thought of doing using if(choice == 1) to loopback!

  • #6
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I'm glad I could help.Actually, the most important change is

    Code:
    while(loop)
    instead of

    Code:
    while(!done)
    Last edited by DistantJob; 02-04-2011 at 09:47 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •