嘿,我正在尝试为我的英语课圣诞晚会构建一个测验程序。它有一个 GUI 等等,但这并不重要。我有三个按钮,每个按钮都带有答案 A、B 或 C 的标签。 我有一个 Button Go,它可以填充数组中的第一个问题。所有其他问题也存储在一个数组中,但每个问题都存储在不同的数组中。它们被命名为 frage 1-10。它们包含问题的三个可能答案和一个告诉程序哪一个是正确答案的字符串。我需要一个名为 nextQuestion 的服务,它将把下一个数组填充到相应的字段中。我对这项服务很挣扎,因为我想不出任何方法来做到这一点。我将在这里发布我的源代码,希望你能帮助我。由于我是德国人,代码包含多语言部分。如果您难以理解,请随时询问,我会尽力解释我的简洁代码。 我预先感谢您的支持。
import sum.komponenten.*;
import sum.werkzeuge.*;
import sum.ereignis.*;
public class DasQuiz extends EBAnwendung
{
// Objekte
private Etikett hatEtikettHeader;
private Knopf hatKnopfAnswerA;
private Knopf hatKnopfExit;
private Knopf hatKnopfAnswerB;
private Knopf hatKnopfAnswerC;
private Zeichenbereich hatZeichenbereichQuestion;
private Etikett hatEtikett2;
private Etikett hatEtikett3;
private Etikett hatEtikett4;
private Etikett hatEtikettTeamOne;
private Etikett hatEtikettTemaTwo;
private Etikett hatEtikettPunkte1;
private Etikett hatEtikettPunkte2;
private Knopf hatKnopfGo;
private String[] frage1 = new String[]{"What is traditionally hidden in a Christmas pudding?", "A Coin", "A Watch", "A pair of dice", "A"};
private String[] frage2 = new String[]{"How many days are there on a traditional advent calendar?", "12", "24", "30", "B"};
private String[] frage3 = new String[]{"____pull Santa's sleigh", "Guniea Pigs", "Cats", "Reindeers", "C"};
private String[] frage4 = new String[]{"According to the song, how many days of Christmas are there?", "11", "12", "13", "B"};
private String[] frage5 = new String[]{"Why do we shorten Christmas to Xmas?", "X is the first letter of the Greek word for Christ", "X looks like a snowflake", "X looks like a star", "A"};
private String[] frage6 = new String[]{"What is the last day of Christmas called?", "Advent", "Twelfth Night", "Boxing Day", "B"};
private String[] frage7 = new String[]{"Are used to make traditional Christingles", "Peaches", "Pears", "Oranges", "C"};
private String[] frage8 = new String[]{"Charles _____ wrote the book A Christmas Carol", "Dahl", "Rowling", "Dickens", "C"};
private String[] frage9 = new String[]{"Which royal couple started off the fashion for Christmas trees in Britain?", "King Henry adn Anne Boleyn", "Queen Victoria adn Prince Albert", "Prince William and kate Middleton", "B"};
private String[] frage10 = new String[]{"Which of these plants is traditionally connected with Christmas?", "Daisy", "Daffodil", "Poinsettia", "C"};
StringBuilder builder = new StringBuilder();
// Attribute
/**
* Konstruktor
*/
public DasQuiz()
{
//Initialisierung der Oberklasse
super(1255, 876);
hatEtikettHeader = new Etikett(468, 57, 225, 25, "Christmas Quiz LK English by Marc Blaesche");
// Ausrichtung
hatEtikettHeader.setzeAusrichtung(Ausrichtung.LINKS);
hatKnopfAnswerA = new Knopf(289, 472, 100, 25, "Answer A");
hatKnopfAnswerA.setzeBearbeiterGeklickt("hatSchalterAnswerAGeklickt");
hatKnopfExit = new Knopf(1078, 810, 100, 25, "Exit");
hatKnopfExit.setzeBearbeiterGeklickt("hatKnopfExitGeklickt");
hatKnopfAnswerB = new Knopf(514, 472, 100, 25, "Answer B");
hatKnopfAnswerB.setzeBearbeiterGeklickt("hatSchalterAnswerBGeklickt");
hatKnopfGo = new Knopf (514, 397, 100, 25, "GOGO");
hatKnopfGo.setzeBearbeiterGeklickt("hatKnopfGoGeklickt");
hatKnopfAnswerC = new Knopf(755, 472, 100, 25, "Answer C");
hatKnopfAnswerC.setzeBearbeiterGeklickt("hatSchalterAnswerCGeklickt");
hatZeichenbereichQuestion = new Zeichenbereich(288, 143, 566, 143, "");
hatEtikett2 = new Etikett(289, 300, 566, 25, "");
// Ausrichtung
hatEtikett2.setzeAusrichtung(Ausrichtung.LINKS);
hatEtikett3 = new Etikett(289, 340, 566, 25, "");
// Ausrichtung
hatEtikett3.setzeAusrichtung(Ausrichtung.LINKS);
hatEtikett4 = new Etikett(289, 380, 566, 25, "");
// Ausrichtung
hatEtikett4.setzeAusrichtung(Ausrichtung.LINKS);
hatEtikettTeamOne = new Etikett(72, 585, 100, 25, "Team One");
// Ausrichtung
hatEtikettTeamOne.setzeAusrichtung(Ausrichtung.LINKS);
hatEtikettTemaTwo = new Etikett(356, 586, 100, 25, "Tema Two");
// Ausrichtung
hatEtikettTemaTwo.setzeAusrichtung(Ausrichtung.LINKS);
hatEtikettPunkte1 = new Etikett(72, 671, 100, 141, "");
// Ausrichtung
hatEtikettPunkte1.setzeAusrichtung(Ausrichtung.LINKS);
hatEtikettPunkte2 = new Etikett(355, 669, 96, 141, "");
// Ausrichtung
hatEtikettPunkte2.setzeAusrichtung(Ausrichtung.LINKS);
}
public void hatKnopfAnswerAGeklickt()
{
}
public void hatKnopfExitGeklickt()
{
System.exit(0);
}
public void hatKnopfAnswerBGeklickt()
{
// Schreiben Sie hier den Text ihres Dienstes
}
public void hatKnopfAnswerCGeklickt()
{
// Schreiben Sie hier den Text ihres Dienstes
}
public void hatKnopfGoGeklickt()
{
hatZeichenbereichQuestion.setzeInhalt(frage1[0]);
hatEtikett2.setzeInhalt(frage1[1]);
hatEtikett3.setzeInhalt(frage1[2]);
hatEtikett4.setzeInhalt(frage1[3]);
}
public void nextQuestion()
{
}
}
最佳答案
Anupam 在评论中所说的是正确的;我相信最简单/最快的方法是将您的 frage
数组合并到一个二维数组中,您可以迭代该数组以获得下一个问题。
private String[][] questions = {frage1, frage2, frage3, frage4, frage5, frage6, frage7}
此外,您的代码需要知道哪个问题是“下一个问题”。有几种方法可以解决这个问题,但是由于我们处理的是数组,一种方法是维护“索引”值,如下所示:
public class DasQuiz extends EBAnwendung {
private int currentQuestionIndex = 0;
...
然后,您的 nextQuestion
方法只需使用该值来确定下一个要加载的问题
例如:
public void nextQuestion() {
String[] currentQuestion = questions[currentQuestionIndex];
hatZeichenbereichQuestion.setzeInhalt(currentQuestion[0]);
hatEtikett2.setzeInhalt(currentQuestion[1]);
hatEtikett3.setzeInhalt(currentQuestion[2]);
hatEtikett4.setzeInhalt(currentQuestion[3]);
currentQuestionIndex++; // increment to the next question index
}
此外,您的 hatKnopfGoGeklickt
方法可以简单地使用 nextQuestion
,而不是自行完成工作:
public void hatKnopfGoGeklickt() {
nextQuestion();
}
旁注#1:请注意,我没有检查 nextQuestion
中的任何数组越界问题,因为我不确定您的程序在以下情况下如何工作它到达了问题的结尾(也许这是显而易见的,不确定,因为我的德语有点生疏)。
旁注#2:如果您创建一个代表问题及其相应答案的类,您可能会发现代码更加简洁。
更新
关于您在评论中所说的不确定如何创建新类(class),这样的事情会很好而且简单:
public class Question { // Frage
private String questionText;
private String[] answers;
private String correctOption;
public Question(String questionText, String[] answers, String correctOption){
...
}
}
关于带 GUI 的 Java 测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27366982/