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 4 of 4

Thread: Please help me how to fix the loop

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Please help me how to fix the loop

    My codes are below, I'm trying to program a guessing game. The computer will generate a random number for each game, and the user will guess what number it is for infinite number of tries. The game has to be repeated if the user wanted to.

    Here is my problem, whenever I press 1 to repeat and guess the number, the errors occur. Can someone please test this and help me fix my error, thank you.





    import java.util.Scanner;
    import java.util.Random;
    public class FourthGradedProgram {


    public static void main (String args []){

    Scanner keyboard = new Scanner (System.in);
    Random number = new Random();
    double userGuess = 0.0;
    boolean right = false;
    double totalGuess = 0.0;
    double continueGame = 0.0;
    double numberOfGames = 0.0;


    do
    {
    int computerNumber = number.nextInt(100) + 1;

    numberOfGames++;

    double guessesEachGame = 0.0;



    do
    {
    System.out.println("The computer's number is "+computerNumber+"");

    System.out.print("Please guess a number between 1 and 100: ");
    userGuess = keyboard.nextDouble();

    guessesEachGame ++;
    totalGuess++;

    {

    if (userGuess == computerNumber)
    {
    right = true;
    }
    else if (userGuess > computerNumber)
    {
    System.out.println("Your guess is too high, please try a lower number!");
    }
    else if (userGuess < computerNumber)
    {
    System.out.println("Your guess is too low, please try a higher number!");
    }
    }
    } while (right == false);


    System.out.println("The computer's number is "+computerNumber+"");

    System.out.println("Your guess is right, the total number of guess is "+guessesEachGame+"");

    System.out.println("Do you wish to continue? Type 1 then press enter to continue and press any other number to stop the game.");

    continueGame = keyboard.nextDouble();


    } while (continueGame == 1);
    System.out.println("The total of guess for all games is "+totalGuess+"");


    System.out.println("Your overall average number of guesses is "+totalGuess/numberOfGames+"");

    } // end main
    } // end class


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Please help me how to fix the loop

    5

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help me how to fix the loop

    Thank you for your help! I figured out the error. It was because I assigned a boolean variable outside of the do loop, so the true and false statement didn't work for the 2nd try. Now that I assigned the boolean variable inside the first do loop, it works perfectly.

  4. #4
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Please help me how to fix the loop

    H

Similar Threads

  1. [SOLVED] A little help please~~How should i fix it??
    By Li Yin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 6th, 2012, 10:12 AM
  2. Need help playing sound in backround of a program in a loop.(Please Fix my code!)
    By DusteroftheCentury in forum What's Wrong With My Code?
    Replies: 30
    Last Post: January 21st, 2012, 02:14 AM
  3. Can someone tell me how to fix this please
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 9th, 2011, 10:51 AM
  4. anyone know how to fix this ?
    By skyzred in forum Collections and Generics
    Replies: 7
    Last Post: June 22nd, 2011, 03:16 AM
  5. Please Help Me Fix
    By keygiwawah in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 25th, 2009, 03:37 AM