gpt4 book ai didi

java - 尝试使用 Netbeans 在 Java 中制作下一个按钮 - 问答游戏

转载 作者:行者123 更新时间:2023-12-02 02:57:30 26 4
gpt4 key购买 nike

我做了一个问答游戏,但我在使用“下一步”按钮时遇到了问题。下一个按钮必须转到下一个问题和下一个答案。第一次执行下一步按钮后,它将转到第一个问题和答案,但不会继续第二个问题和答案。

guiandtests3.java 包 guiandtests3;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Guiandtests3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, ParseException {
gui3 form = new gui3();
form.setVisible(true);

Random random = new Random();
int a = random.nextInt(3+1-0)+0;
int b = random.nextInt(3+1-0)+0;
int c = random.nextInt(3+1-0)+0;
int d = random.nextInt(3+1-0)+0;


JSONParser parser = new JSONParser();
Object obj = null;
obj = parser.parse(new FileReader("c:\\Matematika.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("data");

Iterator<Object> iterator = array.iterator();
//form.s11();
while (iterator.hasNext() ) {
//System.out.println("a");
if (form.getter()==form.g11()){
//System.out.println("abc");
JSONObject element=(JSONObject)iterator.next();
JSONArray abc = (JSONArray) element.get("jawaban");
Iterator<Object> iterator1 = abc.iterator();
ArrayList<String> results = new ArrayList<>();

System.out.println(element.get("soal"));

form.setquestion((String) element.get("soal"));


while (iterator1.hasNext()) {

results.add(iterator1.next().toString());
//System.out.println(iterator1.next());
}


System.out.println(results.get(0));
System.out.println(results.get(1));

while(a==b){
b = random.nextInt(3+1-0)+0;
}
while(a==c | b ==c){
c = random.nextInt(3+1-0)+0;
}
while(a==d | b ==d |c==d){
d = random.nextInt(3+1-0)+0;
}
form.setAnswer1(results.get(a));
form.setAnswer2(results.get(b));
form.setAnswer3(results.get(c));
form.setAnswer4(results.get(d));

form.setanswer(results.get(0));
form.s11();
}

}

}

}

gui3.java

package guiandtests3;
import javax.swing.JOptionPane;
/**
*
* @author abc
*/
public class gui3 extends javax.swing.JFrame {

public String question;
public String answer1;
public String answer2;
public String answer3;
public String answer4;

private int i=0;
private int aa=0;
private String correctanswer="empty";

public gui3() {
initComponents();
}

public void setanswer(String answer){
correctanswer = answer;
}

public String getanswer(){
return correctanswer;
}

public void setter(){
this.i = this.i +1;
}

public int getter(){
return this.i;
}

public void s11(){
this.aa=this.aa+1;
}
public int g11(){
return this.aa;
}

public void setAnswer1(String answer){
this.answer1=answer;
}

public String getAnswer1(){
return this.answer1;
}

public void setAnswer2(String answer){
this.answer2= answer;
}

public String getAsnwer2(){
return this.answer2;
}

public void setAnswer3(String answer){
this.answer3= answer;
}

public String getAsnwer3(){
return this.answer3;
}

public void setAnswer4(String answer){
this.answer4= answer;
}

public String getAsnwer4(){
return this.answer4;
}

public void setquestion(String question){
this.question=question;
}

public String getquestion(){
return this.question;
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("jLabel1");

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("jButton3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText("jButton4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setText("Next");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(176, 176, 176)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(156, 156, 156)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2)
.addComponent(jButton1)
.addComponent(jButton3)
.addComponent(jButton4))))
.addGap(0, 161, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton5)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)
.addGap(18, 18, 18)
.addComponent(jButton4)
.addGap(18, 18, 18)
.addComponent(jButton5)
.addContainerGap(34, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(rootPane,getanswer(),null,JOptionPane.ERROR_MESSAGE);
if(jButton1.getText() == getanswer() ){
JOptionPane.showMessageDialog(rootPane,"Correct",null,JOptionPane.ERROR_MESSAGE);
}else{
JOptionPane.showMessageDialog(rootPane,"Wrong",null,JOptionPane.ERROR_MESSAGE);
}
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(jButton2.getText() == getanswer() ){
JOptionPane.showMessageDialog(rootPane,"Correct",null,JOptionPane.ERROR_MESSAGE);
}else{
JOptionPane.showMessageDialog(rootPane,"Wrong",null,JOptionPane.ERROR_MESSAGE);
}
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if(jButton3.getText() == getanswer() ){
JOptionPane.showMessageDialog(rootPane,"Correct",null,JOptionPane.ERROR_MESSAGE);
}else{
JOptionPane.showMessageDialog(rootPane,"Wrong",null,JOptionPane.ERROR_MESSAGE);
}
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
if(jButton4.getText() == getanswer() ){
JOptionPane.showMessageDialog(rootPane,"Correct",null,JOptionPane.ERROR_MESSAGE);
}else{
JOptionPane.showMessageDialog(rootPane,"Wrong",null,JOptionPane.ERROR_MESSAGE);
}
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText(question);
jButton1.setText(answer1);
jButton2.setText(answer2);
jButton3.setText(answer3);
jButton4.setText(answer4);
setter();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(gui3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(gui3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(gui3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(gui3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new gui3().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}

json 文件

{
"data":[
{
"id":"1",
"soal":"3 * 3 + 2 =",
"jawaban":[
"11",
"15",
"17",
"19"
]

},
{
"id":"2",
"soal":"20 / 5 =",
"jawaban":[
"4",
"3",
"2",
"1"
]
},
{
"id":"3",
"soal":"16 + 6 = ",
"jawaban":[
"22",
"21",
"20",
"24"
]
},
{
"id":"4",
"soal":"2 * 4 =",
"jawaban":[
"8",
"6",
"10",
"12"
]
},
{
"id":"5",
"soal":"1+4*2",
"jawaban":[
"8",
"10",
"12",
"14"
]
},
{
"id":"6",
"soal":"Jika A+1=10 , B+A=11 C= ?",
"jawaban":[
"EGP",
"1",
"12",
"22"
]
},
{
"id":"7",
"soal":"44/11",
"jawaban":[
"4",
"2",
"5",
"8"
]
},
{
"id":"8",
"soal":"1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1 = ",
"jawaban":[
"16",
"15",
"17",
"10"
]
},
{
"id":"9",
"soal":"Berapakah jawaban sebelumnya ",
"jawaban":[
"16",
"17",
"18",
"19"
]
}
]
}

press to see the GUI

最佳答案

您需要将问题和答案传递到 gui 对象中,以便迭代器实际上可以移动到下一个“结果”。

您应该在下一个按钮中移动到下一个结果,而不是从主函数中移动,因为您的 gui 无法访问这些问题,并且 gui 中的任何内容都无法从您在主函数中从数据中收集的结果中获取信息文件。

考虑将文件的初始读取从主函数移至“gui3”对象构造函数,并使“结果”ArrayList 可用作 gui 的实例变量。

第一次读取 JSON 文件时,您使用的随机数会让人非常困惑,但您需要找到一种方法,将问题与单个数组中的答案对齐。

示例:

ArrayList<ArrayList<String>>  results = new ArrayList<ArrayList<String>>();
ArrayList<String> question = new ArrayList<>()
question.add(question);
question.add(answer1);
question.add(answer2);
question.add(answer3);
question.add(answer4);
results.add(questions);

然后,当您按下“下一步”按钮时,您可以

jLabel1.setText(results.get(i).get(0));
jButton1.setText(results.get(i).get(1));
jButton2.setText(results.get(i).get(2));
jButton3.setText(results.get(i).get(3));
jButton4.setText(results.get(i).get(4));
i++; // Or use your setter() function?

根据评论中关于如何将 JSON 放入数组列表的问题,它与您现在的做法非常相似,但您需要将其“添加”到 ArrayList 而不是立即使用它。

类似于:

JSONParser parser = new JSONParser();
Object obj = null;
obj = parser.parse(new FileReader("c:\\Matematika.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("data");
ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
ArrayList<String> question = new ArrayList<>();

Iterator<Object> iterator = array.iterator();
while (iterator.hasNext() ) {
String questionString = element.get("soal");
question.add(questionString);
JSONArray answers = (JSONArray) jsonObject.get("jawaban");
Iterator<String> iterator2 = answers.iterator();
int count = 0;
//The following limits it to pulling back 4 answers from the JSON jawaban element
while (iterator2.hasNext() && count < 4) {
String answer = iterator2.next();
question.add(answer);
count++;
}
results.add(question);
}

将此代码放入 gui3 对象的构造函数中 - 或将其传递到 gui3 构造函数中,并使构造函数的结果如下:

public class gui3 extends javax.swing.JFrame {
ArrayList<ArrayList<String>> results;

//Your other code here

public gui3(ArrayList<ArrayList<String>> results) {
this.results = results;
initComponents();
}

// Your other code here
}

请注意,您需要将此结果 ArrayList 置于类实例范围内,因此您需要将声明移至构造函数之外。

关于java - 尝试使用 Netbeans 在 Java 中制作下一个按钮 - 问答游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42864816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com