Topic: 求助一题

  Print this page

1.求助一题 Copy to clipboard
Posted by: xxyxq163
Posted on: 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+" = ?");
  }
}

2.Re:求助一题 [Re: xxyxq163] Copy to clipboard
Posted by: hubmygirl
Posted on: 2007-09-12 00:12

package tarema.com.cn;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;

public class Test_Awt extends JApplet implements ActionListener {

  int m = 0, n = 0, inputNumber, value;

  JLabel inputLabel;
  JTextField inputField;
  JButton beginButton;

  private boolean flag = false;

  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");
    container.add(beginButton);
    
    
    beginButton.addActionListener(this);
    
  }

  public void actionPerformed(ActionEvent e) {

    //    if ( e.getActionCommand().equals())
//    System.out.println ( e.getActionCommand() );
    if ( inputField.getText().equals("")&& flag == false ){
      flag = true ;
      value = product();
      displayMessage();
      return ;
    }  
    
//    System.out.println ( value );
    
    inputNumber = Integer.parseInt(inputField.getText());
    System.out.println ( inputNumber );
    
    if (value == inputNumber){
      flag = false;
      inputField.setText("");
      showStatus("good");
    }
    else{
      showStatus("Try again");
      inputField.setText("");
      flag = false;
    }
  }

  public int product() {
    m = 1 + (int) (Math.random() * 9);
    n = 1 + (int) (Math.random() * 9);
    return m * n;
  }

  public void displayMessage() {
    showStatus(m + " * " + n + " = ?");
  }
  public static void main ( String [] args) {
    new Test_Awt().init();
  }
}
//呵呵 ,你看看,你的程序会有异常,最简单的方法就是跳过此异常, 在出现之前返回!

3.Re:求助一题 [Re: xxyxq163] Copy to clipboard
Posted by: long_5281
Posted on: 2007-11-09 09:54

我来贴个命令行的:
public class InputMain {

public static void main(String[] args){
DataInputStream in = null ;
InputMain main = new InputMain();
try{
System.out.print("are you ready(y/n):");//是否准备好答题
in = new DataInputStream(System.in);
String readStr = in.readLine();
if(readStr!=null&&!"".equals(readStr)){//判断输入的字符串是否为空
if(readStr.equalsIgnoreCase("y")){ //开始答题
while(true){
int[] struts = main.getRandoms();
outer:
for(;trueWink{
main.showStruts(struts);
String readStr1 = in.readLine();
if(readStr1!=null&&!"".equals(readStr1)){ //判断输入的答案字符串是否为空
if(readStr1.equalsIgnoreCase("quit")){ //如果输入的是QUIT则退出程序
System.exit(0);
}
int src = Integer.parseInt(readStr1);//将字符串转换为整数
int value = main.getValue(struts[0],struts[1]); //获取题目的正确答案
if(main.isTrue(value,src)){ //判断输入的答案与正确答案是否相符
System.out.println("Very good!");
break outer; //如果答题正确则重新出题
}else{
System.out.println("No. Please try again.");
}
}
}
}
}else if(readStr.equalsIgnoreCase("n")){ //退出答题
System.exit(0);
}
}
if(in!=null) in.close(); //关闭输入流
}catch(IOException e){
System.out.println("系统运行出现异常:"+e.getMessage());
}
}

/**
* 获取随机正数数组
* @return
*/
public int[] getRandoms(){
int[] rands = new int[2];
rands[0] = (int) (Math.random()*100) ;
rands[1] = (int) (Math.random()*100) ;
return rands ;
}

/**
* 获取两正数乘积
* @param m
* @param n
* @return
*/
public int getValue(int m,int n){
return m*n;
}

/**
* 判断输入的答案与正确答案是否相符
* @param value
* @param src
* @return
*/
public boolean isTrue(int value,int src){
return value==src;
}

/**
* 显示题目
* @param randoms
*/
public void showStruts(int[] randoms){
System.out.print(randoms[0]+"*"+randoms[1]+"=?");
}
}

4.Re:求助一题 [Re: hubmygirl] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2007-11-11 08:11

hubmygirl wrote:
//呵呵 ,你看看,你的程序会有异常,最简单的方法就是跳过此异常, 在出现之前返回!


Ho..... this is a very very bad idea.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923