public String getAnswer(int i) { try { String rightanswer = reader.get(6 * i + Integer.parseInt(reader.get(6 * i + 5))); System.out.println(rightanswer); } catch (IndexOutOfBoundsException e) { } return rightanswer; }
时间: 2024-02-18 07:05:47 浏览: 20
这段代码实现了获取指定题目的正确答案的功能。具体来说,这段代码的功能是:
1. 根据题目序号`i`计算出正确答案在数据文件中的位置。
2. 从数据文件中读取正确答案,并将其存储在`rightanswer`变量中。
3. 如果读取过程中发生了越界异常,则不进行任何操作。
4. 返回正确答案。
需要注意的是,在这个方法中,`rightanswer`变量是在`try`代码块中定义的,而在`try`代码块之外的代码中也要使用`rightanswer`变量,因此需要将`rightanswer`定义为方法级别的变量,而不是局部变量。修改后的代码如下所示:
```java
private String rightanswer;
public String getAnswer(int i) {
try {
rightanswer = reader.get(6 * i + Integer.parseInt(reader.get(6 * i + 5)));
System.out.println(rightanswer);
} catch (IndexOutOfBoundsException e) {
}
return rightanswer;
}
```
在这个修改后的代码中,我将`rightanswer`变量的定义从`try`代码块中移到了方法级别,这样就可以在`try`代码块之外的代码中使用它了。
相关问题
public String getAnswer(int i) { return rightanswer = reader.get(6*i + Integer.parseInt(reader.get(6*i+5))); }这个不对吗
这个方法看起来有点问题。首先,方法名应该是`getRightAnswer`而不是`getAnswer`,因为这个方法只返回正确答案。其次,在方法中,`rightanswer`不应该作为返回值,而应该将正确答案存储在一个变量中并返回该变量。最后,方法中的索引计算似乎也有点问题,因为第二个索引应该是`6*i+4`而不是`6*i+5`。可能的重构代码如下:
```
public String getRightAnswer(int i) {
String answer = reader.get(6*i + 4);
return answer != null ? answer.trim() : "";
}
```
button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { i++; next.setEnabled(true); if (button2.getText().equals(rightanswer)) { right++; score.setText(right + "/" + i); show.setText("correct!"); button2.setBackground(Color.green); } else { show.setText("wrong"); score.setText(right + "/" + i); button2.setBackground(Color.pink); } rightanswer = this.getAnswer(i);; unenabled(); next.setEnabled(true); } private String getAnswer(int i) { // TODO Auto-generated method stub return null; } });怎么把getanswer移到actionlisterner外面,在方法内部调用?
你可以将 `getAnswer()` 方法移到 `ActionListener` 外面,然后在 `ActionListener` 内部调用该方法。代码如下:
```java
private String getAnswer(int i) {
// TODO Auto-generated method stub
return null;
}
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i++;
next.setEnabled(true);
if (button2.getText().equals(rightanswer)) {
right++;
score.setText(right + "/" + i);
show.setText("correct!");
button2.setBackground(Color.green);
} else {
show.setText("wrong");
score.setText(right + "/" + i);
button2.setBackground(Color.pink);
}
rightanswer = getAnswer(i);
unenabled();
next.setEnabled(true);
}
});
```
注意,在 `ActionListener` 内部调用 `getAnswer()` 方法时,不需要使用 `this.getAnswer()`,直接使用 `getAnswer()` 即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)