本帖最后由 woshiliupingping 于 2010-08-02 14:31:44 编辑

解决方案 »

  1.   

    首先第一个分析的题是有问题的,你用TextListener是不行的,这样每次你输入一个东西,不管是符号还是数字它都会有反应,这样就起不到你分析的效果。
    我改了一下,你可以看一下,如下:import java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class love {
    public static void main(String args[]) {
    WindowFrame s = new WindowFrame("计算的窗口");
    }
    }class WindowFrame extends Frame implements TextListener, ActionListener {
    float aver, sum = 0;
    TextArea a, b;
    Button button1,button2; WindowFrame(String s) {
    super(s);
    a = new TextArea(15, 16);
    b = new TextArea(15, 45);
    button1 = new Button("退出");
    button2 = new Button("分析");
    add(button1);
    add(button2);
    setLayout(new FlowLayout());
    add(a);
    add(b);
    button1.addActionListener(this);
    button2.addActionListener(this);
    //a.addTextListener(this);
    setVisible(true);
    validate();
    setBounds(0, 0, 345, 600);
    } public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(button2)){
    try {
    String s;
    s = a.getText();
    sum = 0;
    StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
    int n = fenxi.countTokens();
    String c[] = new String[n];
    for (int i = 0; i <= n - 1; i++) {
    String temp = fenxi.nextToken();
    System.out.println(temp);
    c[i] = temp;
    }
    for (int j = 0; j < n; j++) {
    sum = sum + Float.parseFloat(c[j]);
    aver = sum / n;
    b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver);
    }
    }

    catch (NumberFormatException ee) {
    }
    }
    else if (e.getSource().equals(button1)){
    System.exit(0);
    }
    } public void textValueChanged(TextEvent e) {
    try {
    String s;
    s = a.getText();
    StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
    int n = fenxi.countTokens();
    String c[] = new String[n];
    for (int i = 0; i <= n - 1; i++) {
    String temp = fenxi.nextToken();
    System.out.println(temp);
    c[i] = temp;
    }
    for (int j = 0; j < n; j++) {
    sum = sum + Float.parseFloat(c[j]);
    aver = sum / n;
    b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver);
    }
    } catch (NumberFormatException ee) {
    }
    }
    }