[英]checking if answer right or wrong
I have a project which is an quiz that has multiple questions, true/false and fill in the blank, my problem is with the Check answer method.我有一个项目,它是一个有多个问题的测验,对/错并填空,我的问题是检查答案方法。
First I have written the Q's in database with SQL and the columns is as follow enter image description here首先,我用 SQL 在数据库中编写了 Q,列如下在此处输入图像描述
and there are 2 methods the first one is for retrieving the questions, choices and right answer有两种方法,第一种是检索问题、选择和正确答案
public void m() {
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/quiz?user=root&password=Manager@1");
Statement stm = con.createStatement();
ResultSet rs1 = stm.executeQuery("Select * from quiz.qs where qid="+ Random());
if (rs1.next()) {
bg.clearSelection();
quest.setText(rs1.getString(2));
r1.setText(rs1.getString(3));
r2.setText(rs1.getString(4));
r3.setText(rs1.getString(5));
r4.setText(rs1.getString(6));
answer = rs1.getString(7);
check(answer);
}
}
catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
the second one is for checking if the chosen answer was right, that's the part that I have problem with第二个是检查选择的答案是否正确,这是我有问题的部分
public void check(String ans) {
boolean works=false;
String stuans="";
if (r1.isSelected()) {
if (ans.equals(r1.getText())) {
marks=marks+1;
}
} else if (r2.isSelected()) {
if (ans.equals(r2.getText())) {
marks=marks+1;
}
} else if (r3.isSelected()) {
if (ans.equals(r3.getText())) {
marks=marks+1;
}
} else if (r4.isSelected()) {
if (ans.equals(r4.getText())) {
marks=marks+1;
}
}
System.out.println(marks);// to check if marks has increased after checking
}
whenever I choose the right answer, the marks does not increase.每当我选择正确答案时,分数不会增加。
enter image description here在此处输入图像描述
that's a pic from the program with GUI, sorry if something is not so clear这是带有 GUI 的程序的图片,如果有什么不清楚的地方,请见谅
Don't use == with Strings.不要将 == 与字符串一起使用。 Use the.equals method.
使用.equals 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.