平均字长java小程序
我有一个任务,用户输入的文本被分析成单词的长度,以及该长度出现的频率。我已经这样做了,但是现在我需要计算用户输入的任何内容的平均长度?这是我目前掌握的代码:平均字长java小程序,java,applet,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.swi
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();
}
}
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入java.awt.event.ActionListener;
导入java.io.*;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共类赋值\u tauqeer\u abbasi扩展JApplet实现ActionListener{
JTextArea textInput;//用户输入。
JLabel wordCountLabel;//显示字数。
公共void init(){
//这里的代码是小程序的自定义代码,包括背景颜色、文本颜色、文本背景颜色、标签和按钮
挫折背景(颜色:黑色);
getContentPane().setBackground(颜色为.black);
textInput=新的JTextArea();
textInput.setBackground(颜色:白色);
JPanel南部=新JPanel();
南部。退根地(颜色:暗灰色);
南部。设置布局(新流程布局(-1));
/*创建分析和重置按钮*/
JButton countButton=新JButton(“分析”);
countButton.addActionListener(此);
添加(计数按钮);
JButton resetButton=新JButton(“重置”);
resetButton.addActionListener(此);
添加(重置按钮);
JButton fileButton=新JButton(“分析文本文件”);
addActionListener(这个);
添加(文件按钮);
/*告诉用户做什么或程序输出什么的标签*/
wordCountLabel=新的JLabel(“字数:”);
wordCountLabel.setBackground(颜色:黑色);
设置前景(颜色为红色);
wordCountLabel.setOpaque(true);
添加(wordCountLabel);
/*小程序的边框*/
getContentPane().setLayout(新的BorderLayout(2,2));
/*用户将在其中输入他们希望分析的文本的文本区域的滚动条*/
JScrollPane scroller=新的JScrollPane(textInput);
getContentPane().add(滚动条,BorderLayout.CENTER);
getContentPane().add(south,BorderLayout.south);
}//end init();
公共插图getInsets(){
//边缘周围的边框大小。
返回新的插图(2,2,2,2);
}
//小程序定制结束
//文本分析开始
// }};
//文本分析结束
public void actionPerformed(java.awt.event.ActionEvent e){
String command=e.getActionCommand();
if(command.equals(“Analyze”)){
字符串[]数组=textInput.getText().split(“”);
int maxWordLength=0;
int字长=0;
for(int i=0;i最大字长){
maxWordLength=字长;
}
}
int[]intArray=newint[maxWordLength+1];
for(int i=0;i”,在数组[i],i中);
}
输出。打印(“”);
wordCountLabel.setText(sw.toString());
}else if(命令等于(“重置”)){
textInput.setText(“”);
setText(“字数:”);
textInput.requestFocus();
}
}
}
我将如何计算用户输入内容的平均长度
我们将非常感谢您的帮助 由于这是一项作业,我将给您一些指导,您将通过自己编写代码学到最多。这里需要的直觉是,您正在创建一个程序来实现其定义的算法。西格玛相当于for循环,在循环的每个过程中都有一个“加法”操作。由于您有一个包含所有单词的数组,并且您知道每个单词的长度,因此需要编写实现该算法的代码:
0的sumValue
开始
length
添加到sumValue
1.类名称(
assignmenttauqeerabasi
)应使用CamelCase,字段通常应为private
。2.为什么不简单地将所有字长相加并存储,然后将输入的字数除以:meanWordLength=wordLengthSum/nrOfWordsEntered
?太好了,我会尝试一下,非常感谢