xxyxq163
发贴: 2
积分: 0
|
于 2007-07-26 19:50
编写一程序帮助小学生学习乘法,利用Math.random产生两个正的一位整数,在状态栏中显示出题目,比如:How much is 6 times 7 ?学生在此JTextField中输入结果,程序检查学生的答案,如果答案正确,打印出"Very good!"并再出一道题,如果答案错误,打印字符"No. Please try again."再让学生重做该题,直到做对为止,利用一个单独的方法来生成题目.当执行applet以及学生每次正确做答时,就调用一次该方法,所有applet中的绘图都用paint方法完成.本人编写时没有在paint中调用,总是有问题,每次输入正确答案,状态栏都显示不正确,请哪位高人帮忙看看,再此先谢了. import java.awt.*;import java.awt.event.*;import javax.swing.*; public class xiti6_31 extends JApplet implements ActionListener {
int m=0,n=0,inputNumber,value; JLabel inputLabel; JTextField inputField; JButton beginButton; public void init() { Container container=getContentPane(); container.setLayout(new FlowLayout()); inputLabel=new JLabel("Enter a key "); container.add(inputLabel); inputField=new JTextField(10);
container.add(inputField); beginButton=new JButton("Begin"); beginButton.addActionListener(this); container.add(beginButton); } public void actionPerformed(ActionEvent e) { value=product(); displayMessage(); inputNumber=Integer.parseInt(inputField.getText()); if(value==inputNumber) showStatus("good"); else showStatus("Try again"); inputField.setText(""); } public int product() { m=1+(int)(Math.random()*9); n=1+(int)(Math.random()*9); return m*n; } public void displayMessage() { showStatus(m+" * "+n+" = ?"); } }
|