我制作一个测验程序,调用两个.txt文件(一次一个)来获取问题和答案。这两个文件有不同数量的问题和答案。它按顺序通过它们,在你得到最后一个问题的答案并点击获取问题按钮后,它会给出索引超出范围的错误。我试图找出在显示最后一个问题后让jbuttonQ被禁用的最佳方法。我想我错过了一个for循环或尝试捕获某个地方。问题和答案是单独的ArrayList
这里是jbuttonQ动作执行的代码:
statusMessageLabel.setText("");
jtxtA.setText("");
jtxtQ.setText(q.getQuestion());
jtxtCurrQ.setText(String.valueOf(q.getQNumber()));
jbtnA.setEnabled(true);
jbtnA.requestFocusInWindow();
jbtnQ.setEnabled(false);
这里是jbuttonA动作执行的代码:
statusMessageLabel.setText("");
jtxtA.setText(q.getAnswer());
jbtnQ.setEnabled(true);
jbtnQ.requestFocusInWindow();
jbtnA.setEnabled(false);
这里是jbtnMenu代码:
statusMessageLabel.setText("");
jtxtQ.setText("");
jtxtA.setText("");
jtxtCurrQ.setText("");
jtxtTotQ.setText("");
JFileChooser f = new JFileChooser(".");
String path = "";
f.setDialogTitle("Select quiz file.");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text file (*.txt)","txt");
f.setFileFilter(filter);
JDialog dg = new JDialog();
int rval = f.showOpenDialog(dg);
if(rval == JFileChooser.CANCEL_OPTION){
statusMessageLabel.setText("Open canceled.");
jtxtFile.setText("");
}else{
path = f.getSelectedFile().getAbsolutePath();
jtxtFile.setText(path);
q = new Quiz(path); //instantiation call
if(q.getErrorMsg().isEmpty()){
statusMessageLabel.setText(q.getActionMsg());
jtxtTotQ.setText(String.valueOf(q.getQCount()));
jbtnQ.setEnabled(true);
jbtnQ.requestFocusInWindow();
}else{
statusMessageLabel.setText(q.getErrorMsg());
}
}
}
这是Quiz.java类:
public class Quiz {
private String filename;
private String errmsg, actmsg;
private ArrayList<String> questions;
private ArrayList<String> answers;
private int qCount, qNumber;
private boolean quizover;
public Quiz(String path){
this.errmsg = "";
this.actmsg = "";
questions = new ArrayList<>();
answers = new ArrayList<>();
try{
BufferedReader in = new BufferedReader(new FileReader(path));
String s = in.readLine();
while (s != null){
questions.add(s);
answers.add(in.readLine());
qCount++;
s = in.readLine();
}
in.close();
}catch(IOException e){
this.errmsg = "Unable to open file " + path;
}
if (qCount > 0){
this.actmsg = "Quiz file has " + qCount + " questions.";
qNumber = 1;
quizover = false;
}else if(qCount <= 0){
this.actmsg = "No questions in file: " + path;
qNumber = 0;
quizover = true;
}
}//end of constructor
public String getErrorMsg(){
return this.errmsg;
}
public String getActionMsg(){
return this.actmsg;
}
public int getQCount(){
return this.qCount;
}
public int getQNumber(){
return this.qNumber;
}
public String getQuestion(){
if(this.qCount == 0){
this.errmsg = "Question requested from empty quiz.";
return this.errmsg;
}
return this.questions.get(this.qNumber - 1);
}
public String getAnswer(){
if(this.qCount == 0){
this.errmsg = "Answer requested from empty quiz.";
return this.errmsg;
}
String a = this.answers.get(this.qNumber - 1);
qNumber ++;
return a;
}
}
任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
我知道你还没有分享完整的代码;所以,我此时对你的代码的理解是有限的。
但是,最大的问题是你正在使用this.qCount检查“最后一个问题”,但你得到的是this.qNumber;你还需要减少qNumber。试试这样的事情:
public String getQuestion(){
if (this.qNumber > 0) {
return this.questions.get(this.qNumber - 1);
}
else {
this.errmsg = "Question requested from empty quiz.";
return this.errmsg;
}
}
public String getAnswer(){
if (this.qNumber > 0) {
String a = this.answers.get(this.qNumber - 1);
qNumber--;
return a;
}
else {
this.errmsg = "Answer requested from empty quiz.";
return this.errmsg;
}
}
答案 1 :(得分:0)
我想通了,我改变了jbtnA代码:
statusMessageLabel.setText("");
if(String.valueOf(q.getQNumber()).equals(String.valueOf(q.getQCount()))){
statusMessageLabel.setText("End of questions in file.");
jbtnQ.setEnabled(false);
}else{
jtxtA.setText(q.getAnswer());
jbtnQ.setEnabled(true);
jbtnQ.requestFocusInWindow();
jbtnA.setEnabled(false);
}