求解java扫雷递归的时候出现错误 看了2小时也不知道错在哪

baidu_26657439 2015-03-17 10:52:19
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.*;
import java.awt.geom.Dimension2D;
import java.util.*;
import javax.swing.*;
public class mine extends JFrame{
/**
*
*/
private static final long serialVersionUID = 111111;
public static int mine_num,bomb_num;
JButton[] an;
JButton kaishi=new JButton("游戏开始");
JButton an_dia;
int bomb[][]=new int[9][9];
int flag[]=new int[100];
JPanel mb1=new JPanel();
JPanel mb2=new JPanel();
JMenuBar menubar=new JMenuBar();
JMenu menu1=new JMenu("游戏");
JMenu menu2=new JMenu("帮助");
JMenuItem menuitem1=new JMenuItem("新游戏");
JMenuItem menuitem3=new JMenuItem("关于");
JMenuItem menuitem2=new JMenuItem("设置");

//JButton[][] an;

public static void main(String[]args){
mine mine1=new mine();
mine1.setVisible(true);

}
public mine(){
this.setTitle("扫雷");//设置窗口标题
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//所有程序必要的
this.setResizable(false);
menubar.add(menu1);
menubar.add(menu2);
menu1.add(menuitem1);
menu1.add(menuitem2);
menu1.add(menuitem3);
an_dia=new JButton("设置雷的数量");
an_dia.setBounds(10, 10, 50, 50);
mb2.setLayout(new GridLayout(9,9));
mb1.setLayout(new FlowLayout(0));
mb1.setBackground(Color.lightGray);
mb2.setBackground(Color.cyan);
an=new JButton[81];
for( int i=0;i<81;i++){
an[i]=new JButton("");
an[i].setFont(new Font(null,Font.BOLD,25));
an[i].setMargin(new Insets(0,0,0,0));
an[i].addActionListener(new danji());
/*an[i].setText("1");
an[i].setEnabled(false);
*/
mb2.add(an[i]);
}
mb1.add(an_dia);
mb1.add(kaishi);
this.setJMenuBar(menubar);
this.add(mb1,BorderLayout.NORTH);
this.add(mb2,BorderLayout.CENTER);
this.setSize(700,625);//设置大小 顺序很重要
kaishi.addActionListener(new reset());
menuitem1.addActionListener(new reset());
an_dia.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //经常出现的错误,把参数ActionEvent e 写成 ActionListener e

new dialog(mine.this); //注意这里的TestDialog.this
}
});
menuitem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //经常出现的错误,把参数ActionEvent e 写成 ActionListener e
new dialog(mine.this); //注意这里的TestDialog.this
}
});
mine_num=20;
reset a=new reset();
a.reset1();
}
//dialog用于获取雷得数量并将其传递给bomb_num。
class dialog extends JDialog{
/**
*
*/
private static final long serialVersionUID = 1L;

public dialog(mine frame){
super(frame,"设置有多少雷",true);
this.setBounds(200, 50, 300, 150);
JPanel m1,m2,m3;
final JTextField jt=new JTextField("20",20);
m1=new JPanel();
m2=new JPanel();
m3=new JPanel();
this.setLayout(new GridLayout(3,1));
JButton an=new JButton("确定");
m1.add(new JLabel("请输入雷的数量(大于5且小于50)"));
m3.add(an);
m2.add(jt);
this.add(m1);
this.add(m2);
this.add(m3);
an.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
mine.mine_num=Integer.valueOf(jt.getText());//整形
Object[] options = { "OK", "CANCEL" };
JOptionPane.showMessageDialog(null, "设置完成", "设置完成", JOptionPane.INFORMATION_MESSAGE);
}
});
this.setVisible(true);//顺序
}
}
class danji implements ActionListener{
int j;
public void kongzhi(int i){
this.j=i;
if(flag[j]==0){
if(bomb[j/9][j%9]!=0){
an[j].setText(""+bomb[j/9][j%9]+"");
flag[j]=1;
an[j].setEnabled(false);
}
else{
if(j==0){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j+1);
this.kongzhi(j+9);
}
else if(j==8){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-1);
this.kongzhi(j+9);
}
else if(j==72){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j+1);
this.kongzhi(j-9);
}
else if(j==80){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-1);
this.kongzhi(j-9);
}
else if(j>=1&&j<=7){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j+1);
this.kongzhi(j-1);
this.kongzhi(j+9);
}
else if(j>=73&&j<=81){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j+1);
this.kongzhi(j-1);
this.kongzhi(j-9);
}
else if(j%9==0){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-9);
this.kongzhi(j+9);
this.kongzhi(j+1);
}
else if(j%9==8){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-9);
this.kongzhi(j+9);
this.kongzhi(j-1);
}
else{
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-9);
this.kongzhi(j-1);
this.kongzhi(j+1);
this.kongzhi(j+9);
}

}
}
}
public void actionPerformed(ActionEvent e){
for(int i=0;i<81;i++){
if(e.getSource()==an[i]){
if(bomb[i/9][i%9]==9){
JOptionPane.showMessageDialog(null, "你踩到雷了", "踩到雷了", JOptionPane.ERROR_MESSAGE);
}
else
this.kongzhi(i);
}
}
}
}
class reset implements ActionListener{
public void reset1(){
bomb_num=mine_num;
for(int i=0;i<81;i++){
flag[i]=0;
an[i].setEnabled(true);
an[i].setText("");
bomb[i/9][i%9]=0;
}
for(int i=0;i<9;i++)
for(int j=0;j<9;j++){
bomb[i][j]=0;
}
for(int i=0;i<mine_num;i++){
int x=(int) (Math.random()*9);
int y=(int) (Math.random()*9);
if(bomb[x][y]!=9)
bomb[x][y]=9;
else
i--;
}
if(bomb[0][0]!=9){
if(bomb[0][1]==9)
bomb[0][0]++;
if(bomb[1][1]==9)
bomb[0][0]++;
if(bomb[1][0]==9)
bomb[0][0]++;
}
if(bomb[0][8]!=9){
if(bomb[0][7]==9)
bomb[0][8]++;
if(bomb[1][8]==9)
bomb[0][8]++;
if(bomb[1][7]==9)
bomb[0][8]++;
}
if(bomb[8][0]!=9){
if(bomb[7][0]==9)
bomb[8][0]++;
if(bomb[8][1]==9)
bomb[8][0]++;
if(bomb[7][1]==9)
bomb[8][0]++;
}
if(bomb[8][8]!=9){
if(bomb[7][7]==9)
bomb[8][8]++;
if(bomb[7][8]==9)
bomb[8][8]++;
if(bomb[8][7]==9)
bomb[8][8]++;
}
for(int i=0;i<9;i++)
for(int j=0;j<9;j++){
if(i>=1&&j>=1&&i<=7&&j<=7){
if(bomb[i][j]!=9){
if(bomb[i-1][j-1]==9)
bomb[i][j]++;
if(bomb[i-1][j+1]==9)
bomb[i][j]++;
if(bomb[i][j-1]==9)
bomb[i][j]++;
if(bomb[i][j+1]==9)
bomb[i][j]++;
if(bomb[i+1][j-1]==9)
bomb[i][j]++;
if(bomb[i+1][j]==9)
bomb[i][j]++;
if(bomb[i+1][j+1]==9)
bomb[i][j]++;
if(bomb[i-1][j]==9)
bomb[i][j]++;
}
}
else if(i==0&&j!=0&&j!=8&&bomb[i][j]!=9){
if(bomb[i][j-1]==9)
bomb[i][j]++;
if(bomb[i][j+1]==9)
bomb[i][j]++;
if(bomb[i+1][j-1]==9)
bomb[i][j]++;
if(bomb[i+1][j]==9)
bomb[i][j]++;
if(bomb[i+1][j+1]==9)
bomb[i][j]++;
}
else if(i==8&&j!=0&&j!=8&&bomb[i][j]!=9){
if(bomb[i][j-1]==9)
bomb[i][j]++;
if(bomb[i][j+1]==9)
bomb[i][j]++;
if(bomb[i-1][j-1]==9)
bomb[i][j]++;
if(bomb[i-1][j]==9)
bomb[i][j]++;
if(bomb[i-1][j+1]==9)
bomb[i][j]++;
}
else if(j==0&&i!=0&&i!=8&&bomb[i][j]!=9){
if(bomb[i-1][j]==9)
bomb[i][j]++;
if(bomb[i+1][j]==9)
bomb[i][j]++;
if(bomb[i-1][j+1]==9)
bomb[i][j]++;
if(bomb[i][j+1]==9)
bomb[i][j]++;
if(bomb[i+1][j+1]==9)
bomb[i][j]++;
}
else if(j==8&&i!=0&&i!=8&&bomb[i][j]!=9){
if(bomb[i-1][j]==9)
bomb[i][j]++;
if(bomb[i+1][j]==9)
bomb[i][j]++;
if(bomb[i-1][j-1]==9)
bomb[i][j]++;
if(bomb[i][j-1]==9)
bomb[i][j]++;
if(bomb[i+1][j-1]==9)
bomb[i][j]++;
}
}
}
public void actionPerformed(ActionEvent e){
this.reset1();
}
}
}



[color=#FF0000]错误如下Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
at mine$danji.kongzhi(mine.java:129)
at mine$danji.kongzhi(mine.java:193)
at mine$danji.kongzhi(mine.java:190)
at mine$danji.kongzhi(mine.java:190)
at mine$danji.kongzhi(mine.java:190)
at mine$danji.kongzhi(mine.java:190)

...全文
174 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
飏飏一蝶 2015-03-18
  • 打赏
  • 举报
回复
编译运行没问题……
三仙半 2015-03-18
  • 打赏
  • 举报
回复
这么多代码,也没有格式,看的好晕啊
baidu_26657439 2015-03-18
  • 打赏
  • 举报
回复
你把雷设置成0 点击就出现错误了 没有一次全部清除
数据集可视化效果可参见下方展示。 【数据集概况】 · 检测类别(中文):[保龄球(bowling)] · 训练集:594 张 · 验证集:75 张 · 测试集:74 张 · 总计:743 张 该数据集聚焦于室内保龄球馆场景,系统性采集了多角度、多姿态下保龄球在不同运动阶段的视觉特征,为保龄球运动过程中的球体识别与轨迹分析提供了高质量标注样本,具有明确的体育训练与智能辅助系统开发价值。... 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 100 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9938** mAP50-95 | 0.6966 Precision | 0.9740 Recall | 0.9974 train/box_loss | 0.9113 train/cls_loss | 0.2862 val/box_loss | 1.1516 val/cls_loss | 0.3116 【训练过程分析】 100 轮训练后 mAP50 达到 0.9938,模型收敛良好。Loss 曲线前段快速下降,后段趋于平稳,val_loss 无反弹,没有明显过拟合。但 mAP50-95 为 0.6966,和 mAP50 差距 0.30,定位精度仍有优化空间。 【模型性能评估】 Precision 0.9740、Recall 0.9974,精召双高,模型对保龄球的检测能力强。 【预测效果展示】 验证集预测效果较好,检测框基本准确覆盖保龄球,置信度整体偏高。 【改进建议】 1. 丰富场景多样性:补充不同光照、背景和遮挡条件下的样本。 2. 提升输入分辨率:640 ...
PaddleOCR 与YOLO目标检测 HTTP 服务。 项目通过 PaddleOCROnnx 原生库集成 OnnxRuntime加速能力,围绕 ONNX 模型部署, 以统一接口提供图像文字识别、YOLO 目标检测和 Tensor 数据输出。 功能概览 文字识别:支持图片 Base64、multipart/form-data 上传,以及文本和 JSON 结果。 目标检测:支持 YOLO 图片检测,返回检测框 JSON 或原始 Tensor。 浏览器演示:访问服务根地址,上传图片并切换 OCR、YOLO 模式。 健康检查:通过 /health 查看服务及 OCR、YOLO 引擎初始化状态。 并发处理:通过 OCR 引擎实例池处理并发请求,可调整实例数量。 体验与调用 启动后访问: 入口 地址 浏览器演示 http://localhost:5000/ 健康检查 http://localhost:5000/health 原生依赖 Windows:优先从 runtimes/win-x64/native/ 加载原生 DLL;该目录没有 PaddleOCROnnx.dll 时,尝试从可执行文件所在目录加载。主 DLL 与对应后端依赖应放在同一原生目录中,不要将同一组依赖分散到多个目录。 Linux:原生运行时位于 runtimes/linux-x64/native/,程序使用相对于可执行文件的运行时搜索路径查找随包部署的依赖。 后端选择:CoreOCROnnx 支持 ONNX Runtime、OpenVINO、TensorRT 后端。请使用与平台、进程架构、硬件和后端匹配的一整套运行时,不要混用不同后端或版本的依赖。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧