gpt4 book ai didi

Java编程-错误跟踪

转载 作者:行者123 更新时间:2023-12-01 06:56:57 25 4
gpt4 key购买 nike

我有一个抽奖程序,曾经有很多命令和组件等。但我想通过省略许多命令或组件(例如按钮或标签)来使其变得更简单。当我尝试这样做时,出现了错误(42 个错误),所有这些错误都显示:

error: class, interface or enum expected.

有人可以帮我找出这些错误吗?

顺便说一句,这是我的代码(已更新):

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.sql.*;

public class Case extends JFrame implements ActionListener
{

static Case app=new Case();
JDesktopPane desk=new JDesktopPane();

Connection cn=null;
ResultSet rs=null;
Statement stat=null;

JInternalFrame log=new JInternalFrame("WELCOME!", true,true,true,true);
JInternalFrame game=new JInternalFrame("Hi! Welcome to RAFFLE DRAW by BSIT22C");

int arr[]=new int[10];
int range=0;

JTextField gametext=new JTextField("",2);
JTextField txtuser=new JTextField("",20);

JMenuBar gamebar=new JMenuBar();
JMenu mnuf=new JMenu("File");
JMenuItem fnew=new JMenuItem("New Game");
JMenuItem fquit=new JMenuItem("Quit Game");
JMenuItem flogout=new JMenuItem("Logout");

JPanel pnllog=new JPanel();
JPanel pnlgame=new JPanel();

JButton blog=new JButton("Login");
JButton bcan=new JButton("Cancel");

JButton bgen=new JButton("Generate Number Set");
JButton bsubmit=new JButton("Submit Number");

JCheckBox[] ck=new JCheckBox[10];

JLabel lblives=new JLabel("");
JLabel lbcons=new JLabel("");

int life=5;
int userscore=0;
int cons=0;


public void init()
{

setContentPane(desk);
desk.add(log);
desk.add(game);

game.setVisible(false);
game.setSize(800,600);
game.setContentPane(pnlgame);
setJMenuBar(gamebar);
gamebar.add(mnuf);
mnuf.add(fnew);
mnuf.add(fscore);
mnuf.add(flogout);
mnuf.add(fquit);

for(int ctr=0;ctr<10;ctr++)
{
ck[ctr]=new JCheckBox();
}
for(int ctr2=0; ctr2<10; ctr2++)
{
pnlgame.add(ck[ctr2]);
}

ck[0].setEnabled(false);
ck[1].setEnabled(false);
ck[2].setEnabled(false);
ck[3].setEnabled(false);
ck[4].setEnabled(false);
ck[5].setEnabled(false);
ck[6].setEnabled(false);
ck[7].setEnabled(false);
ck[8].setEnabled(false);
ck[9].setEnabled(false);

game.setSize(750,100);
pnlgame.add(gametext);
pnlgame.add(bsubmit);
pnlgame.add(bgen);
pnlgame.add(bup);
pnlgame.add(lbtimer);
pnlgame.add(lbtest);
pnlgame.add(lbscore);
pnlgame.add(lbachieve);
pnlgame.add(lblives);
pnlgame.add(lbcons);
lbachieve.setText("Bonus");


bgen.addActionListener(this);
bsubmit.addActionListener(this);

setJMenuBar(gamebar);
gamebar.add(mnuf);
mnuf.add(fnew);
mnuf.add(fscore);
mnuf.add(flogout);
mnuf.add(fquit);

log.setVisible(true);
log.setSize(300,100);
log.setContentPane(pnllog);

pnllog.setLayout(new GridLayout(2,2));
pnllog.add(blog);
pnllog.add(bcan);


setJMenuBar(gamebar);
gamebar.add(mnuf);
mnuf.add(fnew);
mnuf.add(fscore);
mnuf.add(flogout);
mnuf.add(fquit);

fnew.setEnabled(false);
blog.addActionListener(this);
bcan.addActionListener(this);
fnew.addActionListener(this);
fquit.addActionListener(this);
flogout.addActionListener(this);
app.setSize(1024,800);
app.setVisible(true);
}

public void actionPerformed(ActionEvent ex)
{
Object[] options={"Easy", "Normal", "Hard"};

if(ex.getSource()==fnew)
{
bsubmit.setEnabled(false);
gametext.setEditable(false);
int option2=JOptionPane.showOptionDialog(null,"Choose difficulty", "New Game",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
if(option2==0)
{
for(int ctrck=0; ctrck<10; ctrck++)
{
ck[ctrck].setSelected(false);
}
lbtest.setText("");
gametext.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
lbscore.setText("");
life=5;
userscore=0;
bgen.setEnabled(true);
cons=0;
log.setVisible(false);
game.setVisible(true);
range=10;
}
else if(option2==1)
{
for(int ctre=0; ctre<10; ctre++)
{
ck[ctre].setSelected(false);
}
lbtest.setText("");
gametext.setText("");
lbscore.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
life=5;
userscore=0;
bgen.setEnabled(true);
cons=0;
log.setVisible(false);
game.setVisible(true);
range=20;
}
else if(option2==2)
{
for(int ctrn=0; ctrn<10; ctrn++)
{
ck[ctrn].setSelected(false);
}
lbtest.setText("");
gametext.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
lbscore.setText("");
life=5;
userscore=0;
bgen.setEnabled(true);
cons=0;
log.setVisible(false);
game.setVisible(true);
range=40;
}

}
else if(ex.getSource()==fscore)
{
{
int hsconf=JOptionPane.showConfirmDialog(null,"Do you want to view the current score rankings?","Please Confirm",JOptionPane.YES_NO_OPTION);
if(hsconf==0)
{
log.setVisible(false);
game.setVisible(false);
}
else
{

}
}
log.setVisible(false);
game.setVisible(false);
}
else if(ex.getSource()==flogout)
{
int option = JOptionPane.showConfirmDialog(null,"Do you really want to quit playing?", "Do you want to exit?",JOptionPane.YES_NO_OPTION);
if(option==0)
{
for(int q=0; q<10; q++)
{
ck[q].setSelected(false);
}
lbtest.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
lbscore.setText("");
life=5;
userscore=0;
cons=0;
bgen.setEnabled(true);
}
else
{
log.setVisible(true);
}
for(int e=0; e<10; e++)
{
ck[e].setSelected(false);
}
lbtest.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
life=5;
userscore=0;
cons=0;
bgen.setEnabled(true);
log.setVisible(true);
game.setVisible(false);
}
else if(ex.getSource()==fquit)
{
int option = JOptionPane.showConfirmDialog(null,"Do you really want to quit playing?", "Do you want to exit?",JOptionPane.YES_NO_OPTION);
if(option==0)
{
for(int r=0; r<10; r++)
{
ck[r].setSelected(false);
}
lbtest.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
life=5;
userscore=0;
cons=0;
bgen.setEnabled(true);
System.exit(0);
}
else
{
log.setVisible(true);
}
}
else if(ex.getSource()==bcan)
{
int option = JOptionPane.showConfirmDialog(null,"Do you really want to quit playing?", "Do you want to exit?",JOptionPane.YES_NO_OPTION);
if(option==0)
{
System.exit(0);
}
else
{
log.setVisible(true);
}
}

else if(ex.getSource()==bgen)
{
bsubmit.setEnabled(true);
gametext.setEditable(true);
app.generate();
}

else if(ex.getSource()==bsubmit)
{

try
{
app.cmpans();
if(ck[0].isSelected()&&ck[1].isSelected()&&ck[2].isSelected()&&ck[3].isSelected()&&ck[4].isSelected()&&ck[5].isSelected()&&ck[6].isSelected()&&ck[7].isSelected()&&ck[8].isSelected()&&ck[9].isSelected()==true)
{
int win = JOptionPane.showConfirmDialog(null,"Do you want to go for another round?","Congratulations! You WIN!",JOptionPane.YES_NO_OPTION);
if(win==0)
{
for(int r=0; r<10; r++)
{
ck[r].setSelected(false);
}
lbtest.setText("");
life=5;
userscore=0;
gametext.setText("");
lbscore.setText("");
lblives.setText("");
lbcons.setText("");
cons=0;
bgen.setEnabled(true);

log.setVisible(false);
game.setVisible(true);
}
else if(win==1)
{
log.setVisible(false);
game.setVisible(false);
}
}
else if(life<=0)
{
int gameover=JOptionPane.showConfirmDialog(null,"Sorry, you lost!","GAME OVER!",JOptionPane.OK_OPTION);
if(gameover==0)
{
game.setVisible(false);
log.setVisible(false);
}
else if(gameover==1)
{
gametext.setText("");
lbscore.setText("");
lblives.setText("");
lbcons.setText("");
lbtest.setText("");
lbscore.setText("");
bgen.setEnabled(false);
bsubmit.setEnabled(false);
game.setVisible(false);
log.setVisible(false);
}

}
}catch(Exception ebak){}

}

else if(ex.getSource()==blog)
{
fnew.setEnabled(false);
bsubmit.setEnabled(false);
gametext.setEditable(false);
Object[] options1={"Easy", "Normal", "Hard"};
log.setVisible(false);
fnew.setEnabled(true);
int option3=JOptionPane.showOptionDialog(null,"Choose difficulty", "New Game",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, options1[1]);
if(option3==0)
{
for(int ctrck2=0; ctrck2<10; ctrck2++)
{
ck[ctrck2].setSelected(false);
}
lbtest.setText("");
gametext.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
lbscore.setText("");
life=5;
userscore=0;
bgen.setEnabled(true);
cons=0;
log.setVisible(false);
game.setVisible(true);
range=10;
}
else if(option3==1)
{
for(int ctre=0; ctre<10; ctre++)
{
ck[ctre].setSelected(false);
}
lbtest.setText("");
gametext.setText("");
lbscore.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
life=5;
userscore=0;
bgen.setEnabled(true);
cons=0;
log.setVisible(false);
game.setVisible(true);
range=20;
}
else if(option3==2)
{
for(int ctrn=0; ctrn<10; ctrn++)
{
ck[ctrn].setSelected(false);
}
lbtest.setText("");
gametext.setText("");
lbachieve.setText("");
lblives.setText("");
lbcons.setText("");
lbscore.setText("");
life=5;
userscore=0;
bgen.setEnabled(true);
cons=0;
log.setVisible(false);
game.setVisible(true);
range=40;
}

}
}


public void generate()
{
for(int z=0; z<=1; z++)
{
for(int i=0; i<10; i++)
{
Random rdm=new Random();
arr[i] = rdm.nextInt(range)+5;
}
lbtest.setText("Random Numbers: "+arr[0]+"-"+arr[1]+"-"+arr[2]+"-"+arr[3]+"-"+arr[4]+"-"+arr[5]+"-"+arr[6]+"-"+arr[7]+"-"+arr[8]+"-"+arr[9]);
bgen.setEnabled(false);
gametext.setText("");
}

}

public void cmpans()
{
boolean success=false;
boolean boo=false;
String txget;
txget=gametext.getText();
String pars;
int ans;
pars=gametext.getText();
ans=Integer.parseInt(pars);

for(int i=0; i<10; i++)
{

if(ans==arr[i])
{
userscore=userscore+10;
lbscore.setText("Score: "+userscore);
ck[i].setSelected(true);
arr[i]=0000;
gametext.setText("");
lblives.setText("life: "+life);
lbcons.setText("cons: "+cons);
success=true;
}
else
{
boo=true;
}
}

gametext.setText("");
if(success==true)
{
cons++;
if(cons==2)
{
lbachieve.setText("You're a GOOD GUESSER!");
userscore=userscore+20;
}
else if(cons==4)
{
lbachieve.setText("You're an AMAZING GUESSER!!");
userscore=userscore+50;
}
}
else if(boo==true)
{
cons=0;
life--;
lblives.setText("life: "+life);
lbcons.setText("cons: "+cons);
}

}

}
public static void main(String[] args)
{
app.init();
}
}

我无法弄清楚哪一行会导致错误出现。

最佳答案

问题是您的庞大(300多行!)actionPerformed方法末尾有太多右大括号。这意味着您正在关闭类声明,从而导致后续错误。

为了避免将来出现此问题:

  • 让您的 IDE 缩进您的代码。您的缩进从第 156 行开始(就在 option2 的声明下方)以及第 382 行(就在 options1 的声明下方)开始就很奇怪。在其他地方也很奇怪。
  • 让你的方法变得非常非常短。当一个方法开始需要超过一个页面来显示时,您确实应该考虑将其重构为更小的方法。 (理想情况下,在此之前 - 我个人喜欢少于 20 行的方法,尽管我没有严格的限制。) actionPerformed 大小的方法是维护噩梦

关于Java编程-错误跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com