我在问答游戏中遇到了一些问题。我正在使用netbeans。在我的游戏中,我有两个课程,第一个是我的驱动程序类,即 Quiz,第二个是非驱动程序类,即 RadioQuestion。我正在为这两个课程发布我的代码。我的 RadioQuetion 课程中有两个错误。
我在 Quiz.java 中的错误消息是“找不到符号类:RadioQuestion 位置:类 Quiz.quiz”,而在 RadioQuestion 中是“总在 Quiz.quiz 中不公开;不能从外部包访问”和“错误在Quiz.quiz;无法从外部包访问”,一个警告是“实现:java.awt.event.ActionListeneractionPerformed(ActionEvent e)”。
Quiz.quiz 类
package Quiz;
import java.awt.CardLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Quiz extends JFrame{
JPanel p=new JPanel();
CardLayout cards=new CardLayout();
int numQs;
int wrongs=0;
int total=0;
//RadioQuestion[8] questions=new RadioQuestion();
String[][] answers={
{"a","b","c","d"},
{"e","f","g","h"},
{"i","j","k","l"},
{"m","n","o","p"},
{"True","False"},
{"True","False"},
{"4","5","6","7"},
{"q","r","s","t"},
};
RadioQuestion questions[]={
new RadioQuestion("what",answers[0],1,this),
new RadioQuestion("what",answers[1],3,this),
new RadioQuestion("what",answers[2],0,this),
new RadioQuestion("what",answers[3],1,this),
new RadioQuestion("what",answers[4],1,this),
new RadioQuestion("what",answers[5],0,this),
new RadioQuestion("what",answers[6],0,this),
new RadioQuestion("what",answers[7],2,this),
new RadioQuestion("what",answers[8],1,this),
};
public static void main(String[] args) {
//line missing
//line missing
//line missing
new Quiz();
}
public Quiz(){
super("quiz game");
setResizable(true);
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p.setLayout(cards);
numQs=questions.length;
for(int i=0;i<numQs;i++){
p.add(questions[i],"q"+i);
}
Random r=new Random();
int i=r.nextInt(numQs);
cards.show(p,"q"+i);
add(p);
setVisible(true);
}
public void next(){
if((total-wrongs)==numQs){showSummary();}
else{Random r=new Random();
boolean found=false;
int i=0;
while(!found){
i=r.nextInt(numQs);
if(!questions[1].used)
{
found=true;
}}
cards.show(p,"q"+i);
}
}
public void showSummary(){
JOptionPane.showMessageDialog(null,"all done your result"+"\nincorrect: \t"+wrongs+"\n correct ans: \t"+(total-wrongs)+"\n avg inc ans per ques: \t"+((float)wrongs/numQs)+"\n percentage correct: \t\t"+(int)(((float)(total-wrongs)/total)*100)+ "%");
System.exit(0);
}
}
RadioQuestion class
import Quiz.Quiz;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;
//import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class RadioQuestion extends JPanel implements ActionListener {
// private static int EXIT_ON_CLOSE;
//RadioQuestion=new RadioQuestion(String q, String[] options, int ans, Quiz quiz);
int correctAns;
Quiz quiz;
int selected;
boolean used;
//question
JPanel qPanel=new JPanel();
//answers
JPanel aPanel=new JPanel();
JRadioButton[] responses;
ButtonGroup group=new ButtonGroup();
//bottom
JPanel botPanel=new JPanel();
JButton next=new JButton("next");
JButton finish=new JButton("finish");
/*public static void main(String args[]){
JFrame frame=new JFrame("radio button test");
frame.setResizable(true);
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] answers={"wrong1","right","wrong2"};
frame.add(new RadioQuestion("whats right",answers,1));
frame.setVisible(true);
}*/
public RadioQuestion(String q, String[] options, int ans, Quiz quiz){
//dought
this.quiz=quiz;
//dought
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
correctAns=ans;
//question
qPanel.add(new JLabel(q));
//answer
responses=new JRadioButton[options.length];
for(int i=0;i<options.length;i++){
responses[i]=new JRadioButton(options[i]);
responses[i].addActionListener(this);
group.add(responses[i]);
aPanel.add(responses[i]);
}
add(aPanel);
//bottom
next.addActionListener(this);
finish.addActionListener(this);
botPanel.add(next);
botPanel.add(finish);
add(botPanel);
}
////////////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e){
Object src=e.getSource();
//next button
if(src.equals(next)){
showResult();
if(selected==correctAns){
used=true;
quiz.next();}}
//finish button
if(src.equals(finish)){
quiz.showSummary();}
//radio buttons
for(int i=0;i<responses.length;i++){
if(src==responses[i]){
selected=i;}
}}
public void showResult(){
String text=responses[selected].getText();
quiz.total++;
if(selected==correctAns){
JOptionPane.showMessageDialog(null,text+"is correct\nwell done");
}else{
quiz.wrongs++;
JOptionPane.showMessageDialog(null,text+" is wrong\nSorry:(");}
}
//public stataic void main(String[] arg);
}