1

为什么这段代码在运行时不会加载按钮?我知道这个问题非常模糊,但我对 Java 很陌生,并且试图一次理解很多东西。此代码基于我之前两次成功尝试编写井字游戏和连接四的代码,这两种代码都有效。

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);

            }
        }
    } 
}
4

1 回答 1

3

由于运算符的操作数是从左到右计算的,因此在此语句中首先发生整数转换

mines = (int) Math.random()*100;

导致第一项被强制转换为0. 这会导致counter它一增加就减少,从而导致循环无限重复。将操作数括在括号中:

mines = (int) (Math.random() * 100);
于 2013-11-27T18:29:53.080 回答