2014-02-25 57 views
-1

我有这个任务,用户输入的文本被分析到单词的长度以及该长度出现的频率。我已经这样做了,但现在我需要计算用户输入的平均长度。这是我到目前为止的代码:平均单词长度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(); 
     } 
    } 
} 

我将如何解决无论用户输入的平均长度?

任何帮助将不胜感激。

+0

1.你应该使用类名('AssignmentTauqeerAbbasi')和场驼峰通常应该是'private'。 2.为什么不简单地加起来并存储所有的字长,并存储输入和分割的字数:'meanWordLength = wordLengthSum/nrOfWordsEntered'? –

回答

1

既然这是一项任务,我会给你一些指导,你会通过自己编写代码来学习最多。这里需要的直觉是你正在创建一个程序来实现由其mathematical notation定义的算法。 Sigma等效于循环中每次通过时的“加法”操作。既然你的所有单词的数组,你知道每个单词的长度,你需要编写一个实现算法代码:

  1. 开始与sumValue0
  2. 添加每个lengthsumValue
  3. 分结果由您数组的大小
+0

太棒了,生病给我一个尝试,非常感谢 – abdul