此程序的目的是让用户尝试猜测计算机随机选择的最多20个数字。用户输入他们的猜测。然后计算机告诉用户他们的猜测是太高还是太低。用户不断输入他们的猜测直到猜测正确。然后他们被告知他们花了多少猜测。问题是我不明白程序中的算术表达式是否生成从1到20的随机数,因为当我输入1到20之间的数字时,弹出窗口显示“数字太大”#39;
package pkTopic6Exercise13;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Font;
public class Topic6Exercise13
{
private GuessingGame MyGuessingGame = new GuessingGame();
private Topic6Exercise13()
{//constructor
}
public static void main(String[] args)
{
Topic6Exercise13 myTopic6Exercise13 = new Topic6Exercise13();
myTopic6Exercise13.go();
}
private void go()
{
T6Ex13GUIin myT6Ex13GUIin = new T6Ex13GUIin();
}
private class GuessingGame
{
private int NumToGuess=0;
private int Guess=0;
private int NumGuesses=0;
private GuessingGame()
{//constructor
}
private void GenerateNum()
{
NumToGuess = 1+(int)(20*Math.random());
}
private String CheckNum()
{
NumGuesses = NumGuesses + 1;
if (Guess > NumToGuess)
{
JOptionPane.showMessageDialog(null, Guess + " is too BIG!");
return "NotGuessed";
}
else if (Guess < NumToGuess)
{
JOptionPane.showMessageDialog(null, Guess + " is too SMALL!");
return "NotGuessed";
}
else
{
JOptionPane.showMessageDialog(null, "Well done! " + Guess +" is correct, it took " + NumGuesses + " goes.");
return "Guessed";
}
}
}
private class T6Ex13GUIin extends JFrame
{
private JLabel lblGuess;
private JTextField txfGuess;
private JButton btnPickNumber;
private JButton btnQuit;
private T6Ex13GUIin()
{//constructor
Font fontDialog = new Font("Dialog", Font.BOLD,24);
this.setSize(1000,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(100,0);
this.setTitle("Guessing Game");
this.setLayout(null);
lblGuess = new JLabel("Type Guess:");
lblGuess.setFont(fontDialog);
lblGuess.setBounds(100,200,200,50);
lblGuess.setVisible(false);
this.add(lblGuess);
txfGuess = new JTextField("");
txfGuess.setBounds(300,200,100,50);
txfGuess.setFont(fontDialog);
txfGuess.setVisible(false);
ReturnListener MyReturnListener = new ReturnListener();
txfGuess.addKeyListener(MyReturnListener);
this.add(txfGuess);
ClickListener MyClickListener = new ClickListener();
btnPickNumber = new JButton("Pick Number");
btnPickNumber.setBounds(100,100,750,50);
btnPickNumber.setFont(fontDialog);
btnPickNumber.addActionListener(MyClickListener);
this.add(btnPickNumber);
btnQuit = new JButton("Quit");
btnQuit.setBounds(750,300,100,50);
btnQuit.setFont(fontDialog);
btnQuit.addActionListener(MyClickListener);
this.add(btnQuit);
this.setVisible(true);
}
private class ClickListener implements ActionListener
{
private ClickListener()
{//constructor
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnPickNumber)
{
MyGuessingGame.GenerateNum();
MyGuessingGame.NumGuesses=0;
btnPickNumber.setVisible(false);
lblGuess.setVisible(true);
txfGuess.setVisible(true);
txfGuess.setText("");
}
if (e.getSource() == btnQuit)
{
System.exit(0);
}
}
}
private class ReturnListener implements KeyListener
{
private ReturnListener()
{//constructor
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
if (e.getKeyChar() == '\n')
{
MyGuessingGame.Guess =
Integer.parseInt(txfGuess.getText());
if (MyGuessingGame.CheckNum().equals("Guessed"))
{
lblGuess.setVisible(false);
txfGuess.setVisible(false);
btnPickNumber.setVisible(true);
}
else
{
txfGuess.setText("");
}
}
}
}
}
}
答案 0 :(得分:0)
你有没有尝试过任何测试?比如,调试并查看为NumToGuess设置的值是多少?或者只是简单地显示正在设置的值?然后,如果你知道随机数是什么,你可以测试你的逻辑。
根据您的核心逻辑,我在下面运行了这个简单的代码,并没有遇到任何问题。
public static void main(String[] args) {
int NumToGuess = 1+(int)(20*Math.random());
int myGuess = Integer.parseInt("10");
System.out.println("Value is: " + NumToGuess);
System.out.println("My Guess: " + myGuess);
if (myGuess > NumToGuess) {
System.out.println("Too Big");
}
else if (myGuess < NumToGuess) {
System.out.println("Too Small");
}
}
答案 1 :(得分:-1)
Math.rand()
生成0到1之间的随机数。因此,乘以20可得到0到20之间的独占值。通过使用(int)
转换向下舍入,它变为0到19之间,并且通过加1,它变为1-20(含)。是的,您的号码正确生成。但是,您在checkNum()
方法的开头再次添加1,这可能是一个问题。希望这会有所帮助。