2014-02-25 3 views
-1

사용자가 입력 한 텍스트가 단어의 길이와 그 길이가 얼마나 자주 나오는지 분석하는이 과제가 있습니다. 나는 그것을했지만, 이제는 사용자가 입력 한 길이의 평균 길이를 계산해야합니까? 이것은 내가 지금까지 가지고있는 코드입니다 :평균 단어 길이 자바 애플릿

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에 의해 정의 된 알고리즘을 구현하기위한 프로그램을 만들고 있다는 것입니다. 시그마는 루프의 각 패스에서 "추가"연산을 사용하여 for 루프와 동일합니다. 당신은 모든 단어의 배열을 가지고 있고, 각 단어의 길이를 알고 있기 때문에, 당신은 알고리즘을 구현하는 코드를 작성해야하십시오 sumValue0

    1. 시작 추가를 sumValuelength
    2. 분할하여 배열의 크기에 의한 결과는
  • +0

    위대하다, 아프다. – abdul