Java编程、输入数字个数、平均数、最小值、最大值减去最小值、write a JAVA program to read in a sequence of integers and print out the following quantities,each on a new line and in the following order,your program should be:1)
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/02 12:41:01
![Java编程、输入数字个数、平均数、最小值、最大值减去最小值、write a JAVA program to read in a sequence of integers and print out the following quantities,each on a new line and in the following order,your program should be:1)](/uploads/image/z/3689228-20-8.jpg?t=Java%E7%BC%96%E7%A8%8B%E3%80%81%E8%BE%93%E5%85%A5%E6%95%B0%E5%AD%97%E4%B8%AA%E6%95%B0%E3%80%81%E5%B9%B3%E5%9D%87%E6%95%B0%E3%80%81%E6%9C%80%E5%B0%8F%E5%80%BC%E3%80%81%E6%9C%80%E5%A4%A7%E5%80%BC%E5%87%8F%E5%8E%BB%E6%9C%80%E5%B0%8F%E5%80%BC%E3%80%81write+a+JAVA+program+to+read+in+a+sequence+of+integers+and+print+out+the+following+quantities%2Ceach+on+a+new+line+and+in+the+following+order%2Cyour+program+should+be%3A1%29)
Java编程、输入数字个数、平均数、最小值、最大值减去最小值、write a JAVA program to read in a sequence of integers and print out the following quantities,each on a new line and in the following order,your program should be:1)
Java编程、输入数字个数、平均数、最小值、最大值减去最小值、
write a JAVA program to read in a sequence of integers and print out the following quantities,each on a new line and in the following order,your program should be:
1) the number of integers read in.
2) the average value- which need not be an integer.注意,平均值不是实数!
3) the minimum value of the integers.
4) the maximum difference between any of the integers.
以上要求顺序不能颠倒、跪求!
Java编程、输入数字个数、平均数、最小值、最大值减去最小值、write a JAVA program to read in a sequence of integers and print out the following quantities,each on a new line and in the following order,your program should be:1)
这是源代码,直接考到本地即可运行:
package com.cc.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Test test = new Test();
test.print("Input Num Count:");
int count = scan.nextInt();
int[] data = new int[count];
for (int i = 0; i < count; i++) {
data[i] = scan.nextInt();
}
test.output(data);
}
private void print(String info) {
System.out.print(info);
}
private void println(String info) {
System.out.println(info);
}
/**
* 取得首个最小值和最大值.
*
* @param data
*/
private void output(int[] data) {
DataInfo maxData = new DataInfo(Integer.MIN_VALUE, -1);
DataInfo minData = new DataInfo(Integer.MAX_VALUE, -1);
int posTemp = -1;
double total = 0;
for (int dataTemp : data) {
posTemp++;
total += dataTemp;
if (dataTemp > maxData.getValue()) {
maxData.setValue(dataTemp);
maxData.setPos(posTemp);
}
if (dataTemp < minData.getValue()) {
minData.setValue(dataTemp);
minData.setPos(posTemp);
}
}
this.printDataInfo(data, total, maxData, minData);
}
private void printDataInfo(int[] data, double total, DataInfo maxData,
DataInfo minData) {
int length = data.length;
double average = total / length;
this.println("the number of integers read in:" + length);
this.println(" the average value- which need not be an integer:"
+ average);
this.println("the minimum value of the integers:" + minData.getValue()
+ " Pos:" + minData.getPos());
this.println("the maximum difference between any of the integers:"
+ (maxData.getValue() - minData.getValue()));
}
private class DataInfo {
int value;
int pos = -1;
public DataInfo(int value, int pos) {
this.value = value;
this.pos = pos;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getPos() {
return pos;
}
public void setPos(int pos) {
this.pos = pos;
}
}
}