我目前正在开发一个项目,其中生成了0到20之间的2个随机整数。还选择随机运算符(+, - 或*)。
用户必须在3次尝试中输入正确答案。如果他们在第一次尝试时获得它,则获得5分,第二次尝试,3分和第三次尝试,1分。如果在3次尝试之后他们仍然没有得到正确的答案,他们会得到0分,并且会产生一组新的数字和一个运算符。
这是我的问题:当我运行测试仪并输入我的答案时,尝试次数成功增加1,但点数保持为0.我做了一些排查并发现 pt < - (用户拥有的点数)的值保持为0(其初始化值),因为 answer 的值保持为0 ,即使 randomize()方法中的switch语句更改了 answer 的值。
以下是我的项目代码:
第一类(Adder.java):
package adder;
import java.util.Random;
public class Adder
{
int int1 = 0;
int int2 = 0;
int answer = 0;
String operator;
public int randomize()
{
Random rnd1 = new Random();
int1 = rnd1.nextInt(21);
int2 = rnd1.nextInt(21);
int op = rnd1.nextInt(3);
switch(op)
{
case 0:
operator = "+";
answer = int1 + int2;
break;
case 1:
operator = "-";
answer = int1 - int2;
break;
case 2:
operator = "*";
answer = int1 * int2;
break;
}
//Test
System.out.println(int1);
System.out.println(int2);
System.out.println(operator);
System.out.println(answer);
return answer;
}
public static void main(String[] args)
{
//Test
//Adder a = new Adder();
//a.randomize();
}
}
第二类(AdderGame.java):
package adder;
public class AdderGame
{
//1 try = 5 pt, 2 try = 3 pt, 3 try = 1 pt
int numTries = 0;
//Number of points the user has, init to 0
int pt = 0;
Adder checker = new Adder();
public int checkAnswer(int a)
{
System.out.println("CHECKER.ANSWER = " + checker.answer);
if (a == checker.answer)
{
numTries++;
switch(numTries)
{
case 1:
pt = pt + 5;
break;
case 2:
pt = pt + 3;
break;
case 3:
pt++;
break;
}
}
else if (a != checker.answer)
{
//Another test output, to see if this loop is entered or not
System.out.println("INCORRECT");
}
return pt;
}
}
第3类(AdderTester.java)(GUI):
package adder;
public class AdderTester extends javax.swing.JFrame
{
//Create objects from the other two classes
Adder adder = new Adder();
AdderGame checker = new AdderGame();
public AdderTester() {
initComponents();
adder.randomize();
num1Label.setText(Integer.toString(adder.int1));
num2Label.setText(Integer.toString(adder.int2));
opLabel.setText(adder.operator);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
num1Label = new javax.swing.JLabel();
opLabel = new javax.swing.JLabel();
num2Label = new javax.swing.JLabel();
answerTextfield = new javax.swing.JTextField();
confirmButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
ptLabel = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
numTriesLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.setBackground(new java.awt.Color(54, 69, 79));
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Adder");
jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 450, -1));
num1Label.setFont(new java.awt.Font("Dialog", 0, 16)); // NOI18N
num1Label.setForeground(new java.awt.Color(255, 255, 255));
num1Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
num1Label.setText("num1");
jPanel1.add(num1Label, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 100, -1, -1));
opLabel.setFont(new java.awt.Font("Dialog", 0, 16)); // NOI18N
opLabel.setForeground(new java.awt.Color(255, 255, 255));
opLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
opLabel.setText("op");
jPanel1.add(opLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 100, 40, -1));
num2Label.setFont(new java.awt.Font("Dialog", 0, 16)); // NOI18N
num2Label.setForeground(new java.awt.Color(255, 255, 255));
num2Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
num2Label.setText("num2");
jPanel1.add(num2Label, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 100, -1, -1));
answerTextfield.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
jPanel1.add(answerTextfield, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 140, 260, -1));
confirmButton.setFont(new java.awt.Font("Dialog", 0, 16)); // NOI18N
confirmButton.setText("Confirm");
confirmButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
confirmButtonActionPerformed(evt);
}
});
jPanel1.add(confirmButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 310, -1, -1));
jLabel2.setBackground(new java.awt.Color(255, 255, 255));
jLabel2.setFont(new java.awt.Font("Dialog", 0, 16)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Current Points:");
jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 180, -1, -1));
ptLabel.setBackground(new java.awt.Color(255, 255, 255));
ptLabel.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
ptLabel.setForeground(new java.awt.Color(255, 255, 255));
ptLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
ptLabel.setText("pt");
jPanel1.add(ptLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 210, 60, -1));
jLabel3.setBackground(new java.awt.Color(255, 255, 255));
jLabel3.setFont(new java.awt.Font("Dialog", 0, 16)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("Number of Tries:");
jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 240, 170, -1));
numTriesLabel.setBackground(new java.awt.Color(255, 255, 255));
numTriesLabel.setFont(new java.awt.Font("Dialog", 0, 16)); // NOI18N
numTriesLabel.setForeground(new java.awt.Color(255, 255, 255));
numTriesLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
numTriesLabel.setText("numTries");
jPanel1.add(numTriesLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 270, 110, -1));
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 450, 350));
pack();
}// </editor-fold>
private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Holds the value for the answer the user inputs
int userAns = Integer.valueOf(answerTextfield.getText());
//Holds the value for the answer
System.out.println("answer = ");
checker.checkAnswer(userAns);
ptLabel.setText(String.valueOf(checker.pt));
numTriesLabel.setText(String.valueOf(checker.numTries));
}
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(AdderTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AdderTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AdderTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AdderTester.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 AdderTester().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField answerTextfield;
private javax.swing.JButton confirmButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel num1Label;
private javax.swing.JLabel num2Label;
private javax.swing.JLabel numTriesLabel;
private javax.swing.JLabel opLabel;
private javax.swing.JLabel ptLabel;
// End of variables declaration
}
答案 0 :(得分:0)
您正在创建Adder
的两个实例,一个位于AdderGame
,另一个位于AdderTester
。由于它们是不同的对象,因此您的比较在checkAnswer()
中失败。而不是在Adder
中创建AdderGame
的新对象,而是将其作为AdderGame
构造函数的参数。
答案 1 :(得分:0)
您的问题是您在Adder
中宣布了另一个AdderTester
。您在此加法器上调用了randomize
。但是当你检查答案时,你正在检查那个检查器。 (AdderGame
中包含的那个)。以下是解决此问题的方法。
首先,在getAdder
中添加AdderGame
方法:
public Adder getAdder() {
return checker;
}
然后,将AdderTester
的构造函数更改为:
public AdderTester() {
initComponents();
checker.getAdder().randomize();
num1Label.setText(Integer.toString(checker.getAdder().int1));
num2Label.setText(Integer.toString(checker.getAdder().int2));
opLabel.setText(checker.getAdder().operator);
}
并在AdderTester
中删除此行:
Adder adder = new Adder();