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: No idea how to fix it.

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

    Default No idea how to fix it.

    I have a game were the computer tries to guess the users number by using <, >, and = to tell the computer if your number is higher(>) lower(<) or if that is your number(=) . Also, it should keep track of the average number of guesses it takes per game and the number of games you play. But, so far all I can do is get it to run one game before it quits and I need it to be able to run more than one game I think I'm not doing the loop part right, but I have no idea how to fix it.

    Here is what I have so far.....

    import javax.swing.JOptionPane;

    public class TestGame
    {
    public static void main(String[] args)
    {
    int[] games = {0, 0};
    randomGame(games);
    JOptionPane.showMessageDialog(null, "You played " + games[0] + " games with a " + games[1] + " average amount of tries.");
    System.exit(0);
    }
    public static int[] randomGame(int[] array)
    {
    String input;
    int guess, a=50, b=100, c=0, d=0, total=0, attempt=0;
    char repeat;
    {

    JOptionPane.showMessageDialog(null, "Guess a number between 1 and 100 and prepare to start the game.");
    total ++;


    do
    {
    input = JOptionPane.showInputDialog( "How about " + a + "(<,=,>)?");
    attempt ++;

    if (input.equals(">"))
    {
    d = (b - a) / 2 + a;
    c = a;
    a = d;
    }

    else if (input.equals("<"))
    {
    d = a - ((a-c)/2);
    b = a;
    a = d;

    }

    else if (input.equals("="))
    {
    JOptionPane.showMessageDialog(null, "Good game!");
    }

    total =+ attempt;
    }while (!input.equals("="));
    {array[0]++;

    input = JOptionPane.showInputDialog ("Would you like to play again (y/n)?");
    repeat = input.charAt(0);
    }while (repeat == 'y');


    array[1] = (total/array[0]);
    return array;
    }
    }
    }

    Any help at all would be apreciated
    Thank You


  2. #2
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: No idea how to fix it.

    Hello Hammer67, first off it would help a lot if you would place your code
    In one of these boxes and have it formatted with indents and what-not
    so that it is easier to read and therefore easier to spot possible problems. From what I can see, your code exits the loop and when the user chooses to play again the loop never runs again. I would suggest creating a method to hold the loop and you could just call that method when you want to run the game, So you would have an initial call to that method to start the game and then another call if they want to play again. There are other approaches to get the desired result, some are probably better, but this should help you out.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: No idea how to fix it.

    import javax.swing.JOptionPane;
     
    public class TestGame
    {
    public static void main(String[] args)
    {
    int[] games = {0, 0};
    randomGame(games);
    JOptionPane.showMessageDialog(null, "You played " + games[0] + " games with a " + games[1] + " average amount of tries.");
    System.exit(0);
    }
    public static int[] randomGame(int[] array)
    {
    String input;
    int guess, a=50, b=100, c=0, d=0, total=0, attempt=0;
    char repeat;
    {
     
    JOptionPane.showMessageDialog(null, "Guess a number between 1 and 100 and prepare to start the game."); 
    total ++;
     
     
    do
    {
    input = JOptionPane.showInputDialog( "How about " + a + "(<,=,>)?");
    attempt ++; 
     
    if (input.equals(">"))
    {
    d = (b - a) / 2 + a;
    c = a;
    a = d; 
    }
     
    else if (input.equals("<"))
    {
    d = a - ((a-c)/2);
    b = a;
    a = d;
     
    }
     
    else if (input.equals("="))
    {
    JOptionPane.showMessageDialog(null, "Good game!");
    }
     
    total =+ attempt;
    }while (!input.equals("="));
    {array[0]++;
     
    input = JOptionPane.showInputDialog ("Would you like to play again (y/n)?");
    repeat = input.charAt(0);
    }while (repeat == 'y');
     
     
    array[1] = (total/array[0]);
    return array;
    }
    }
    }

    I hope that makes it easier to read

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: No idea how to fix it.

    Your indentation of nested code is poor or completely lacking. You need to indent the statements 3 or 4 spaces for every {}. The ending } should be directly below the line with the starting {. That makes it possible to look at the code and understand the nesting of loops and if statements.
    Your code is all left adjusted. You can not see the line with the starting { when looking up from the ending }.
    For example:
        if (input.equals(">"))
        {
            d = (b - a) / 2 + a;
            c = a;
            a = d; 
        }

Similar Threads

  1. My Idea about this forum...
    By IAmHere in forum Member Introductions
    Replies: 6
    Last Post: October 15th, 2011, 02:14 PM
  2. Blog idea
    By dks in forum Java Theory & Questions
    Replies: 3
    Last Post: February 17th, 2011, 02:48 PM
  3. need help with my idea!
    By frostwing in forum Object Oriented Programming
    Replies: 3
    Last Post: February 10th, 2011, 09:53 AM
  4. Project idea?
    By toop in forum Java Theory & Questions
    Replies: 3
    Last Post: January 6th, 2011, 11:09 PM
  5. No idea what to do for this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 18th, 2010, 07:16 PM

Tags for this Thread