平均字长java小程序

时间:2014-02-25 17:29:42

标签: java applet

我有这个分配,用户输入的文本被分析为单词的长度以及该长度出现的频率。我已经这样做了,但现在我需要计算用户输入的平均长度?这是我到目前为止的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class assignment_tauqeer_abbasi extends JApplet implements ActionListener {

    JTextArea textInput;     // User Input.
    JLabel wordCountLabel;   // To display number of words.

    public void init() {
        // This code from here is the customisation of the Applet, this includes background colour, text colour, text back ground colour, labels and buttons 

        setBackground(Color.black);
        getContentPane().setBackground(Color.black);

        textInput = new JTextArea();
        textInput.setBackground(Color.white);

        JPanel south = new JPanel();
        south.setBackground(Color.darkGray);
        south.setLayout(new FlowLayout(-1));

        /* Creating Analyze and Reset buttons */
        JButton countButton = new JButton("Analyze");
        countButton.addActionListener(this);
        south.add(countButton);

        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(this);
        south.add(resetButton);

        JButton fileButton = new JButton("Analyze Text File");
        fileButton.addActionListener(this);
        south.add(fileButton);

        /* Labels telling the user what to do or what the program is outputting */
        wordCountLabel = new JLabel("  No. of words:");
        wordCountLabel.setBackground(Color.black);
        wordCountLabel.setForeground(Color.red);
        wordCountLabel.setOpaque(true);
        south.add(wordCountLabel);

        /* Border for Applet. */
        getContentPane().setLayout(new BorderLayout(2, 2));

        /* Scroll bar for the text area where the user will input the text they wish to analyse.   */
        JScrollPane scroller = new JScrollPane(textInput);
        getContentPane().add(scroller, BorderLayout.CENTER);
        getContentPane().add(south, BorderLayout.SOUTH);

    } // end init();

    public Insets getInsets() {
        // Border size around edges. 
        return new Insets(2, 2, 2, 2);
    }

    // end of Applet customisation 
    // Text analysis start
    // }};
    // Text analysis end
    public void actionPerformed(java.awt.event.ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("Analyze")) {
            String[] array = textInput.getText().split(" ");
            int maxWordLength = 0;
            int wordLength = 0;
            for (int i = 0; i < array.length; i++) {
                array[i] = array[i].replaceAll("[^a-zA-Z]", "");
                wordLength = array[i].length();
                if (wordLength > maxWordLength) {
                    maxWordLength = wordLength;
                }
            }
            int[] intArray = new int[maxWordLength + 1];
            for (int i = 0; i < array.length; i++) {
                intArray[array[i].length()]++;
            }
            StringWriter sw = new StringWriter();
            PrintWriter out = new PrintWriter(sw);
            out.print("<html>");
            for (int i = 1; i < intArray.length; i++) {
                out.printf("%d word(s) of length %d<br>", intArray[i], i);
            }
            out.print("</html>");
            wordCountLabel.setText(sw.toString());
        } else if (command.equals("Reset")) {
            textInput.setText("");
            wordCountLabel.setText("No of words:");
            textInput.requestFocus();
        }
    }
}

我将如何计算用户输入的平均长度?

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

由于这是一项任务,我会给你一些指导,你会通过自己编写代码来学到最多。这里需要的直觉是你正在创建一个程序来实现由mathematical notation定义的算法。 Sigma相当于你的for循环,在循环的每次传递中都有一个“加法”操作。由于你有一个包含所有单词的数组,并且你知道每个单词的长度,你需要编写实现该算法的代码:

  1. sumValue 0
  2. 开头
  3. 将每个length添加到sumValue
  4. 将结果除以数组的大小