java ----- 数组累加计算器 求平均值
来源:互联网 发布:九亭淘宝仓库合租 编辑:程序博客网 时间:2024/06/06 01:51
MyJFrame.java
import java.awt.Button;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.TextEvent;import java.awt.event.TextListener;import java.text.DecimalFormat;import java.util.StringTokenizer;import javax.swing.JFrame;public class MyJFrame extends JFrame implements TextListener, ActionListener{TextArea text1;TextArea text2;Button button;//MyImageJPanel image;public MyJFrame(){setExtendedState(Frame.MAXIMIZED_VERT);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(450, 150, 100, 200);setSize(500, 500);this.setTitle("计算的窗口");//setLocationRelativeTo(this);Container con = getContentPane();con.setLayout(new FlowLayout());//image = new MyImageJPanel();//image.setBounds(0, 0, this.getWidth(), this.getHeight());text1 = new TextArea();text2 = new TextArea();button = new Button("清空");button.setBackground(Color.lightGray);//con.add(image);con.add(text1);con.add(text2);con.add(button);text2.setEditable(false);text1.addTextListener(this);button.addActionListener(this);setVisible(true);validate();}public void textValueChanged(TextEvent e) {String s = text1.getText();StringTokenizer st = new StringTokenizer(s, " ,'\n'");int n = st.countTokens();String a[] = new String[n];for(int i = 0; i <= n - 1; i++){String temp = st.nextToken();a[i] = temp;}double addResult = 0;double aveResult = 0;for(int i = 0; i < n; i++){addResult += Double.valueOf(a[i]);}if(n != 0){aveResult = addResult / n;}text2.setText(null);text2.append(n + "个数相加的和:" + addResult + "\n");DecimalFormat d = new DecimalFormat("#.00"); text2.append("平均值:" + d.format(aveResult) + "\n");}public void actionPerformed(ActionEvent e) {text1.setText(null);}}
TestJFrame.java
public class TestJFrame {public static void main(String[] args) {MyJFrame jFrame=new MyJFrame();}}
- java ----- 数组累加计算器 求平均值
- java中数组求最大值、最小值、平均值。
- C语言 java 求整形数组平均值
- Java之二维数组求平均值
- 数组求平均值
- Java-求平均值应用程序
- java 1054. 求平均值
- 求数组的和,平均值
- 求数组中奇数平均值?
- iOS数组求和、求平均值
- 动态求二维数组平均值
- Arm汇编求数组平均值
- PHP 二维数组求平均值
- java数组累加
- java 代码笔记 求平均值
- 求二维数组周边值的平均值
- 三维数组求深度的平均值问题
- 3-17(求数组平均值)
- c++中的常量折叠
- fix协议介绍-常用资源
- WPF - MVP模式的几个debug小技巧
- 如何分析竞争对手的SEO
- notepad++使用去掉红线
- java ----- 数组累加计算器 求平均值
- OpenCV学习(五)平滑滤波
- 查询所有的存储过程哪些中包含某个字符串
- Java数组排序Arrays.sort,以及Comparator接口的用法
- cJSON学习笔记
- Linux中查看系统资源占用情况的命令
- OpenStack云计算平台
- Android调试.so库常用工具
- 8个月从CS菜鸟到拿到Google Offer的经历+内推