我正在嘗試創建一個程式,我可以在其中計算 2 個隨機生成的數字并將它們顯示在JLabel. 該程式成功地在 a 中顯示亂數,JLabel但它只計算已生成的第一組數字。
我希望它能夠計算隨機生成的所有數字集。
Random dice = new Random();
boolean solution;
JLabel num1,num2,checker;
int calculation;
public void repeatLoop() {
switch(calculation) {
case 1:
int addition1 = Integer.valueOf(num1.getText()) Integer.valueOf(num2.getText());
break;
case 2:
num1.setText("");
num2.setText("");
break;
case 3:
num1.setText(String.valueOf(1 dice.nextInt(6)));
num2.setText(String.valueOf(1 dice.nextInt(6)));
break;
}
}
public JLabel checkerLabel() {
JLabel checker = new JLabel("");
checker.setFont(new Font("Tahoma", Font.BOLD, 13));
checker.setForeground(Color.GREEN);
checker.setHorizontalAlignment(SwingConstants.CENTER);
checker.setBounds(163, 21, 109, 23);
contentPane.add(checker);
return checker;
}
public addFrame() {
JLabel num1 = new JLabel("3");
num1.setHorizontalAlignment(SwingConstants.CENTER);
num1.setFont(new Font("Tahoma", Font.PLAIN, 20));
num1.setBounds(122, 101, 63, 32);
contentPane.add(num1);
JLabel num2 = new JLabel("4");
num2.setHorizontalAlignment(SwingConstants.CENTER);
num2.setFont(new Font("Tahoma", Font.PLAIN, 20));
num2.setBounds(268, 101, 63, 32);
contentPane.add(num2);
this.num1 = num1;
this.num2 = num2;
calculation = 3;
int addition1 = Integer.valueOf(num1.getText()) Integer.valueOf(num2.getText());
JLabel addOperation = new JLabel(" ");
addOperation.setHorizontalAlignment(SwingConstants.CENTER);
addOperation.setFont(new Font("Tahoma", Font.PLAIN, 20));
addOperation.setBounds(195, 101, 63, 32);
contentPane.add(addOperation);
this.checker = checkerLabel();
textField1 = new JTextField();
textField1.setHorizontalAlignment(SwingConstants.CENTER);
textField1.setBounds(163, 155, 120, 32);
contentPane.add(textField1);
textField1.setColumns(10);
JButton checkBtn1 = new JButton("Check");
checkBtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final int addition2 = addition1;
int ansUser = Integer.parseInt(textField1.getText());
if(addition2 == ansUser) {
solution = true;
} else {
solution = false;
}
if(solution == true) {
checker.setText("CORRECT");
checker.setForeground(Color.GREEN);
} else {
checker.setText("WRONG");
checker.setForeground(Color.RED);
}
textField1.setText("");
calculation = 1;
calculation = 2;
repeatLoop();
}
});
}
uj5u.com熱心網友回復:
您的代碼中有一些邏輯錯誤。我相信我已經在下面的代碼中修復了它們。
我建議您運行以下代碼以確保其行為符合您的要求。如果確實如此,則將其與您的代碼進行比較以查看差異。沒有很多。這是一個摘要:
- 我做
addition1了一個班員。 - 我在方法的陳述句中添加了一條
break陳述句。switchrepeatLoop - 我改變了最后幾行方法
actionPerformed。
最后,我建議你用除錯器運行你的原始代碼,看看你的代碼為什么會出現邏輯問題。
這是代碼。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class addFrame extends JFrame {
private JPanel contentPane;
private JTextField textField1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
addFrame frame = new addFrame();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
Random dice = new Random();
boolean solution;
JLabel num1, num2, checker;
int calculation;
int addition1;
public void repeatLoop() {
switch (calculation) {
case 1:
addition1 = Integer.valueOf(num1.getText()) Integer.valueOf(num2.getText());
break;
case 2:
num1.setText("");
num2.setText("");
case 3:
num1.setText(String.valueOf(1 dice.nextInt(6)));
num2.setText(String.valueOf(1 dice.nextInt(6)));
}
}
public JLabel checkerLabel() {
JLabel checker = new JLabel("");
checker.setFont(new Font("Tahoma", Font.BOLD, 13));
checker.setForeground(Color.GREEN);
checker.setHorizontalAlignment(SwingConstants.CENTER);
checker.setBounds(163, 21, 109, 23);
contentPane.add(checker);
return checker;
}
public addFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel title3 = new JLabel("ADDITION");
title3.setForeground(Color.RED);
title3.setFont(new Font("Verdana", Font.PLAIN, 30));
title3.setHorizontalAlignment(SwingConstants.CENTER);
title3.setBounds(108, 47, 224, 32);
contentPane.add(title3);
JLabel num1 = new JLabel("3");
num1.setHorizontalAlignment(SwingConstants.CENTER);
num1.setFont(new Font("Tahoma", Font.PLAIN, 20));
num1.setBounds(122, 101, 63, 32);
contentPane.add(num1);
JLabel num2 = new JLabel("4");
num2.setHorizontalAlignment(SwingConstants.CENTER);
num2.setFont(new Font("Tahoma", Font.PLAIN, 20));
num2.setBounds(268, 101, 63, 32);
contentPane.add(num2);
this.num1 = num1;
this.num2 = num2;
calculation = 3;
addition1 = Integer.valueOf(num1.getText()) Integer.valueOf(num2.getText());
JLabel addOperation = new JLabel(" ");
addOperation.setHorizontalAlignment(SwingConstants.CENTER);
addOperation.setFont(new Font("Tahoma", Font.PLAIN, 20));
addOperation.setBounds(195, 101, 63, 32);
contentPane.add(addOperation);
this.checker = checkerLabel();
textField1 = new JTextField();
textField1.setHorizontalAlignment(SwingConstants.CENTER);
textField1.setBounds(163, 155, 120, 32);
contentPane.add(textField1);
textField1.setColumns(10);
JButton checkBtn1 = new JButton("Check");
checkBtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final int addition2 = addition1;
int ansUser = Integer.parseInt(textField1.getText());
if (addition2 == ansUser) {
solution = true;
}
else {
solution = false;
}
if (solution == true) {
checker.setText("CORRECT");
checker.setForeground(Color.GREEN);
}
else {
checker.setText("WRONG");
checker.setForeground(Color.RED);
}
textField1.setText("");
calculation = 2;
repeatLoop();
calculation = 1;
repeatLoop();
}
});
checkBtn1.setBounds(183, 211, 89, 23);
contentPane.add(checkBtn1);
setLocationRelativeTo(null);
}
}
uj5u.com熱心網友回復:
也許您應該break在結構中的每個案例之后添加switch(calculation) 。
嘗試將 switch 陳述句修改為:
switch(calculation) {
case 1:
int addition1 = Integer.valueOf(num1.getText()) Integer.valueOf(num2.getText());
break;
case 2:
num1.setText("");
num2.setText("");
break;
case 3:
num1.setText(String.valueOf(1 dice.nextInt(6)));
num2.setText(String.valueOf(1 dice.nextInt(6)));
break;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429893.html
上一篇:如何使用圖形正確實作圖層系統
