Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What's wrong with my code?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question What's wrong with my code?

    Two questions, why doesn't my program let me keep putting in numbers until I guess the right one?

    What is wrong with my loop? Pressing 1 should repeat the program, any other number should close.

    Thanks!

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

    public class HighLowGame
    {
    public static void main(String[] args)
    {

    Random rand = new Random();


    int numberOfTries = 0;
    Scanner input = new Scanner(System.in);
    char repeat;
    char ans;
    int guess;
    boolean win = false;



    do {

    System.out.println("Pick a number between 0 and 10: ");
    guess = input.nextInt();
    numberOfTries++;
    int numberToGuess = rand.nextInt(10);

    if (guess == numberToGuess) {
    win = true;
    System.out.println("Congratulations you have won!");


    }
    else if (guess > numberToGuess) {
    System.out.println("Your guess was high try again");

    }
    else if (guess < numberToGuess) {
    System.out.println("Your guess was low try again");
    }

    System.out.println("The number was " + numberToGuess);
    System.out.println("You guessed the number in "+ numberOfTries + " attempt(s)");
    System.out.println("New game? 1 for Yes, or any other number to quit ");
    ans = input.next().charAt(0);

    } while (ans == 1);








    }
    }


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: What's wrong with my code?

    One problem I notice right away is that if they guess incorrectly it will tell them if they are too low or high, and then give them the answer and ask if they want to try again. if you enter 1 your program will allow you to input more numbers, if you enter something else it won't since it doen't meet the condition.

    Try using a do while to set up the number, and then have a while loop inside the do to ask for an input and test it. It should keep looping indefinitely. If they guess the correct number it should let them know, tell them the amount of attempts it took, and break out of the loop, then it should ask them if they want to continue.

    do
    set random num
    while(condition)
      if(condition)
        print info and break
    print info and ask if they want to continue
    while(condtion)

Similar Threads

  1. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 08:59 AM
  2. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 04:21 AM
  3. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 02:38 AM
  4. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 02:24 PM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM