这是我的第一个Stack问题..所以感谢任何帮助(关于发布样式或给出问题)
所以..我在Java中并不是真正的“新手”,但有一些我不了解的事情。
我对如何存储以下内容有疑问: “这些中的哪一个是水果”,“橙色”,“砖块”,“老虎”,“(以及某种正确答案的表示)”。
请记住,这些记录不会超过20种,所以我会静态地将它们放入手中
我在考虑使用二维数组,但它是如何工作的。我的导师还说2d阵列是最好的方式(我认为即使有更好的方法他也期待2d阵列)......他还说它必须尽可能简单所以任何奇特的方式Java必须暂缓。
我可以管理2d数组部分.. 但是我如何指出正确的答案?某种布尔值是真还是假?
关于从用户那里收集答案并进行检查的部分对我来说不是一个大问题。
我想就你如何处理这项任务提出建议。谢谢!
P.S。它必须是applet可以处理的东西 编辑: 好的伙计..我去惠特2d阵列这里是代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
public class Quiz_applet extends JApplet implements ActionListener {
static int rez = 0;
static int c=0;
static JTextField input = new JTextField(3);
static String[][] data = {//q1 is in field data[0][0]
{"q1", "A1", "A2", "A3", "A"},
{"q2", "A1", "A2", "A3", "B"},
{"q3", "A1", "A2", "A3", "C"},
{"q4", "A1", "A2", "A3", "B"},
{"q5", "A1", "A2", "A3", "C"},
{"q6", "A1", "A2", "A3", "A"},
{"q7", "A1", "A2", "A3", "A"},
{"q8", "A1", "A2", "A3", "B"},
{"q9", "A1", "A2", "A3", "C"},
{"q10", "A1", "A2", "A3", "A"},
{"q11", "A1", "A2", "A3", "A"}
};
public void init() {
ShapeCanvas canvas = new ShapeCanvas();
JButton Button = new JButton("Answer!");
Button.addActionListener(canvas);
input.addActionListener(canvas);
JPanel bottom = new JPanel();
bottom.setLayout(new GridLayout(1,4,3,3));
bottom.add(Button);
bottom.add(input);
getContentPane().setLayout(new BorderLayout(3,3));
getContentPane().add("Center",canvas);
getContentPane().add("South",bottom);
setSize(600,300);
}
static class ShapeCanvas extends JPanel
implements ActionListener {
ShapeCanvas() {
}
public void paintComponent(Graphics g) {
if(c==0){
g.drawString(data[0][0], 50, 100);
g.drawString(data[0][1], 50, 120);
g.drawString(data[0][2], 50, 140);
g.drawString(data[0][3], 50, 160);
}
else if(c!=0){
g.drawString(data[c][0], 50, 100);
g.drawString(data[c][1], 50, 120);
g.drawString(data[c][2], 50, 140);
g.drawString(data[c][3], 50, 160);
//repaint();
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
@Override
public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (command.equals("Answer!"))
{
if(input.getText().equals(data[c][4]))
{
c=+1;
rez=+1;
};
} ;
}
}
单击答案按钮后如何更改问题? 在我的其他项目中,它已经像这样工作^。 p.s.2。我知道网格布局有点“怪异”,但它有效..:)
答案 0 :(得分:0)
确实有更好的方法可以做到这一点,例如使用Map可以让你映射“Orange” - >真/假
但是,如果它必须是2d数组,您可以执行以下操作:
private final String YES = "Y";
private final String NO = "N";
String[20][2] fruitData = new String[20][2]();
fruitData[0][0] = "Orange";
fruitData[0][1] = YES;
fruitData[1][0] = "Brick";
fruitData[1][1] = NO;
答案 1 :(得分:0)
这是一种有意义的对象。这允许您以有意义的方式将问题与答案配对。它不是一个二维阵列,但这里没什么特别的。然后,您可以将这些Question对象存储在一维数组中
public class Question {
String question;
List<String> allAnswers=new ArrayList<>();
String correctAnswer;
public Question(String question,String correctAnswer, List<String> wrongAnswers) {
this.question = question;
this.correctAnswer = correctAnswer;
allAnswers.addAll( wrongAnswers);
//add the correct answer somewhere at random (so that when printing the options it isn't always the first one)
Random rnd=new Random();
allAnswers.add( rnd.nextInt(wrongAnswers.size()), correctAnswer);
}
public boolean isCorrectAnswer(String answer){
return correctAnswer.equalsIgnoreCase(answer);
}
@Override
public String toString() {
//string builder would be better but may constitute "fancy"
String questionAsString= question;
for(String answer: allAnswers){
questionAsString=questionAsString+"\n";
questionAsString=questionAsString+answer;
}
return questionAsString;
}
public static void main(String[] args){
List<String> incorrectAnswers=new ArrayList<>();
incorrectAnswers.add("Brick");
incorrectAnswers.add("Tiger");
Question question=new Question("Which of these is a fruit","Orange", incorrectAnswers);
System.out.println(question);
System.out.println(); //just putting in a blank line
System.out.println(question.isCorrectAnswer("Brick"));
System.out.println(question.isCorrectAnswer("orange"));
}
}
答案 2 :(得分:0)
在2D数组中,您可以将最后一列作为正确答案的索引。然后,您将索引转换为int并检索正确的答案:
<强>格式:强>
问题,choice1,choice2,choice3,正确索引
示例记录:
“其中哪一个是水果”,“橙色”,“砖块”,“老虎”,“1”
“这些是哪辆车”,“海龟”,“大众”,“老鼠”,“2”
在第一个记录中,“1”是“Orange”的数组索引(正确答案)。在第二个记录中,“2”是“大众汽车”的数组索引。