# No idea how to fix it.

• September 12th, 2011, 08:04 PM
Hammer67
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
• September 12th, 2011, 08:40 PM
SerratedMind
Re: No idea how to fix it.
Hello Hammer67, first off it would help a lot if you would place your code
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.
• September 12th, 2011, 10:38 PM
Hammer67
Re: No idea how to fix it.
Code :

```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
• September 13th, 2011, 11:41 AM
Norm
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:
Code :

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