use*_*625 0 java random user-interface parsing jpanel
我写了一个GUI程序来猜测1到200之间的随机数.当我运行它时,我无法正确执行它.我可以猜两次相同的数字,有时它会说"太低",有时它会说"太高".我必须要尝试一些我尝试过的故障,但我很遗憾为什么这不起作用.这是我的代码:
import java.util.Random;
public class GuessPanel extends javax.swing.JPanel {
protected Random random;
protected int x;
protected int n;
public GuessPanel() {
initComponents();
}
@SuppressWarnings("unchecked")
**Generated Code**
private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {
random = new Random();
String s = userField.getText();
int i = 200;
x = random.nextInt(i);
n = Integer.parseInt(s);
if (x == n)
{
answerLabel.setText("You guessed right!!!");
}
else if (x > n)
{
answerLabel.setText("Your guess is too low, guess again");
}
else if (x < n)
{
answerLabel.setText("Your guess is too high, guess again");
}
}
// Variables declaration - do not modify
private javax.swing.JLabel answerLabel;
private javax.swing.JButton guessButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField userField;
// End of variables declaration
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
5302 次 |
| 最近记录: |