- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我做了一个问答游戏,但我在使用“下一步”按钮时遇到了问题。下一个按钮必须转到下一个问题和下一个答案。第一次执行下一步按钮后,它将转到第一个问题和答案,但不会继续第二个问题和答案。
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"
]
}
]
}
最佳答案
您需要将问题和答案传递到 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/
I'm posting this in Q&A style as there are currently a few posts on S/O with similar questions and a
我正在尝试做一些我认为非常简单的事情——传递一个队列和一个函数,并将该函数应用于队列中的每个项目——但我无法编译它。 class Foo { public: void doStuff();
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 5 年前。 Improve th
我的密码使用的是 sha512,但 secret 问题和答案是纯文本。问题是:我需要散列 secret 答案吗?如果是这样,它使用什么数据类型,它仍然是 char(128) 吗?我假设 secret
以前是否有人使用 Freebase 作为知识库在问答机上做过任何工作?我在网上搜索了这个,但没有得到任何实质性的东西。有谁知道在输入非结构化问题并且 QA 引擎利用 Freebase 提供答案的这个领
以前是否有人使用 Freebase 作为知识库在问答机上做过任何工作?我在网上搜索了这个,但没有得到任何实质性的东西。有谁知道在输入非结构化问题并且 QA 引擎利用 Freebase 提供答案的这个领
我正在尝试编写一个您自己的时区转换器,我需要一种方法来确定该月的最后一天可能是哪一天。经过一些研究,我发现了寻找闰年的公式。 这是一个很小的贡献,但也许我会为其他人节省我花 20 分钟弄清楚并应用它的
我有一个 Azure Function(不在容器中)。 当我将其发布到本地 Azure 时,就可以了。 当我从 Azure Devops 发布它时,部署成功,但该函数无法运行。错误“Azure 函数运
当 __getitem__ 映射到内部序列类型时,以下哪一项是推荐的执行方式? class A: def __init__(self, ...): ... se
如何在我的 ng2 应用程序中使用预处理器?我正在使用 angular-cli 和 the original docs对我来说还不够清楚。此外,我想在全局范围内使用这些样式,而不仅仅是在组件范围内。
StackO 的 friend 们大家好。 最后一天,我研究了一种解决方案,将不同表中的特定值导出到一个 XML 文件中。主要问题:我有三层嵌套表。由于我在编写这些函数时遇到了问题,因此我想与您分享我
这是一个问答,这意味着我正在分享我对我遇到的问题的解决方案/答案: 问题是 getting started guide来自 apache 站点的并不完全是最新的,经过一些摆弄之后,我设法让示例工作。
我是一名优秀的程序员,十分优秀!