Minefield Program :: 'else' before 'if error?
Hi guys,
First post here, but I've been viewing the forums for a long time. I had to write this program for my Java II Class in school (I'm in highschool). I can't seem to figure out what the problem is here that is keeping me.
Code Minefield.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Minefield extends JFrame implements ActionListener
{
JPanel[] aPanel = new JPanel[20];
JButton[] aButton = new JButton[20];
int bomb = (int) (Math.random() * 100) % 20;
int counter;
JLabel bLab = new JLabel("");
public Minefield()
{
setTitle("Mine Field");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 5));
for (int i = 0; i<aPanel.length; i++)
{
aPanel[i] = new JPanel(new GridLayout(1, 1));
aButton[i] = new JButton("");
aButton[i].setBackground(Color.BLUE);
add(aPanel[i]);
aPanel[i].add(aButton[i]);
aButton[i].addActionListener(this);
}//end forloop
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
counter++;
for (int i = 0; i<aButton.length; i++)
{
if(aButton[i] == source)
{
if(i == bomb);
{
aButton[i].setBackground(Color.RED);
for (int a = 0; i<aButton.length; a++)
{
if(a != bomb)
{
aButton[a].setBackground(Color.WHITE);
}
}
}
else
{
aButton[i].setBackground(Color.WHITE);
if(counter == 10)
{
bLab.setText("You win!");
}
}
}
}//end forloop
}
public static void main(String[] args)
{
Minefield b1 = new Minefield();
}//END MAIN METHOD
}//END CLASS Minefield
Any help would be very appreciated! Thanks in advance :D
Re: Minefield Program :: 'else' before 'if error?
What is the problem...does it compile? Are there exceptions? Does it behave incorrectly? This information is important for us to help you solve your issue(s)
Re: Minefield Program :: 'else' before 'if error?
You have a semi-colon after this if statement, which counts as the statement for the if. The block after the if then is always executed because it's not part of the if, and Java thinks you're putting an if by itself.