为什么这段代码在运行时不会加载按钮?我知道这个问题很模糊,但我对Java很新,并试图同时理解很多东西。此代码基于我之前两次成功尝试编写TicTacToe和Connect Four的两种方法。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.event.ActionListener;
public class Minesweeper extends Applet implements ActionListener {
// initializing all data types
JButton[] a; // The grid boxes
int counter = 1; // Obsolete
char[] letter; // Array of mine locations
int[] numbers; // Array of numbers; values and locations
boolean explode = false;
String name1;
String name2;
int mines;
public void init() {
// The code below initializes and locates the grid
setLayout(new GridLayout(10, 10));
a = new JButton[100];
// The code below fills the grid with buttons that can be clicked
for (int counter = 0; counter < 100; counter++) {
a[counter] = new JButton();
a[counter].setText(" ");
a[counter].setBackground(Color.white);
a[counter].addActionListener(this);
add(a[counter]);
}
}
public void nit() {
numbers = new int[100];
letter = new char[100];
for (counter = 0; counter < 10; counter++) {
mines = (int) Math.random() * 100;
if (letter[mines] == '*') {
counter--;
} else {
letter[mines] = '*';
}
}
for (counter = 0; counter < 100; counter++) {
numbers[counter] = 0;
}
for (int search = 0; search < 10; search++) {
for (int searchb = 0; searchb < 10; searchb++) {
if (letter[search * 10 + searchb] == '*') {
if (search != 0) {
numbers[((search - 1) * 10) + searchb]++;
}
if (search != 9) {
numbers[((search + 1) * 10) + searchb]++;
}
if (searchb != 0) {
numbers[((search * 10) + searchb) - 1]++;
}
if (searchb != 9) {
numbers[((search * 10) + searchb) + 1]++;
}
if ((search != 0) && (searchb != 0)) {
numbers[(((search - 1) * 10) + searchb) - 1]++;
}
if ((search != 9) && (searchb != 9)) {
numbers[(((search + 1) * 10) + searchb) + 1]++;
}
if ((search != 0) && (searchb != 9)) {
numbers[(((search - 1) * 10) + searchb) + 1]++;
}
if ((search != 9) && (searchb != 0)) {
numbers[(((search + 1) * 10) + searchb) - 1]++;
}
}
}
}
for (int counter = 0; counter < 100; counter++) {
letter[counter] = (char) ('0' + numbers[counter]);
JOptionPane.showMessageDialog(null, " " + letter[counter]);
}
}
// ActionEvent e is the click
public void actionPerformed(ActionEvent e) {
int pickedsquare = 0, coloring, pickedcolumn = 0;
JButton b = (JButton) e.getSource();
counter++;
b.setText("Test");
for (int f = 0; f < 100; f++) {
JOptionPane.showMessageDialog(null, " " + letter[f]);
if (a[f].getText() == "Test") {
pickedsquare = f;
JOptionPane.showMessageDialog(null, " " + letter[f]);
name1 = " " + letter[pickedsquare];
a[f].setText(name1);
break;
}
}
if (letter[pickedsquare] == '*')
explode = true;
if (explode == true) {
JOptionPane.showMessageDialog(null, "You are dead!");
for (int counterb = 0; counterb <= 99; counterb++) {
a[counterb].setEnabled(false);
}
}
if (counter == 89) {
JOptionPane.showMessageDialog(null, "You have swept all mines!");
for (int counterb = 0; counterb <= 99; counterb++) {
a[counterb].setEnabled(false);
}
}
}
}
答案 0 :(得分:3)
由于运算符的操作数是从left to right计算的,因此整数转换首先出现在此语句中
mines = (int) Math.random()*100;
导致第一个词语被转换为0
。这会导致counter
一递增就会递减,从而导致循环重复 ad infinitum 。将操作数括在括号中:
mines = (int) (Math.random() * 100);