//test
public static void main(String[] args) {
int[] ti={12,51,23,4,72,34,7,25,78,61};
cxsz.testNumber(ti);
}
public class cxsz {
//求最小、最大、平均、和
public static void testNumber(int[] par) {
int min = par[0], max = par[0], sum = 0, ave = 0;
for (int i = 0; i < par.length; i++) {
if (min > par[i]) {
min = par[i];
}
if (max < par[i]) {
max = par[i];
}
sum = sum + par[i];
}
ave = sum / par.length;
System.out.println("最小数:" + min);
System.out.println("最大数:" + max);
System.out.println("平均数:" + ave);
System.out.println("和:" + sum);
}
}
run:
最小数:4
最大数:78
平均数:36
和:367
成功生成(总时间:0 秒)
补充你自己做一下,很简单
1 public class Cxsz{
2 public static void main(String[] args){
3 int[] array = new int[]{12,51,23,4,72,34,7,25,78,61};
4 int min=array[0],max=array[0],avg=array[0];
5 int sum = 0;
6 for(int i=0;i<array.length;i++){
7 if(array[i]>max)
8 max = array[i];
9 if(array[i]<min)
10 min = array[i];
11 sum += array[i];
12 }
13 avg = sum/array.length;
14 System.out.println("数组的最大值为:"+max);
15 System.out.println("数组的最小值为:"+min);
16 System.out.println("数组的平均值为:"+avg);
17 }
18 }
下面是 cxsz.java
的代码:
public class cxsz {
public static void main(String[] args) {
int[] arr = {12, 51, 23, 4, 72, 34, 7, 25, 78, 61};
int max = arr[0];
int min = arr[0];
int sum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
sum += arr[i];
}
double average = (double) sum / arr.length;
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
System.out.println("平均值:" + average);
}
}
下面是 TextEvent.java
的代码:
import java.awt.*;
import java.awt.event.*;
public class TextEvent extends Frame {
TextField inputField;
TextField outputField;
public TextEvent() {
setLayout(new FlowLayout());
inputField = new TextField(10);
outputField = new TextField(10);
outputField.setEditable(false);
inputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = inputField.getText();
double num = Double.parseDouble(input);
double result = Math.sqrt(num);
outputField.setText(String.valueOf(result));
}
});
add(inputField);
add(outputField);
setSize(200, 100);
setTitle("文本框事件");
setVisible(true);
}
public static void main(String[] args) {
new TextEvent();
}
}
这些程序分别实现了题目中提到的功能。cxsz.java
求取整数数组的最大值、最小值和平均值,而 TextEvent.java
实现了在一个文本框中输入整数,按回车键后在另一个文本框中显示该数的平方根的功能。