public class ForYou
{
private static Object[] numArr = { 3, 25, 2.1, 16, 14, 2, 19, 91, 87, 102, 0.25, 21, 75, 93, 222, 15, 61, 321,
1024, 525, 879, 12, 27, 3.2, 88.0, 56, 72, 1022, 128, 300, 402, 505, 21.32 };
public static void main(String args[])
{
ForYou fy = new ForYou();
int countInt = 0; //整数个数
int countDouble = 0; //小数个数
double sum = 0;
for (int i = 0; i < numArr.length; i++)
{
if (numArr[i] instanceof Integer)
{
countInt++;
sum += (Integer) numArr[i];
}
if (numArr[i] instanceof Double)
{
countDouble++;
sum += (Double) numArr[i];
}
}
double average = sum / numArr.length; //平均数
System.out.println(fy.getMax().toString()); //最大值
System.out.println(fy.getMin().toString()); //最小值
System.out.println(numArr.length);
System.out.println(average);
System.out.println(countInt);
}
private Object getMax()
{
Double max = 0d;
for (int i = 0; i < numArr.length - 1; i++)
{
Double numDb1 = Double.parseDouble(numArr[i].toString());
Double numDb2 = Double.parseDouble(numArr[i + 1].toString());
if (numDb1 > numDb2)
{
numArr[i + 1] = numDb1;
}
}
max = (Double) numArr[numArr.length - 1];
if (max - max.intValue() > 0)
{
return max;
}
else
{
return max.intValue();
}
}
private Object getMin()
{
Double min = 0d;
for (int i = 0; i < numArr.length - 1; i++)
{
Double numDb1 = Double.parseDouble(numArr[i].toString());
Double numDb2 = Double.parseDouble(numArr[i + 1].toString());
if (numDb1 < numDb2)
{
numArr[i + 1] = numDb1;
}
}
min = (Double) numArr[numArr.length - 1];
if (min - min.intValue() > 0)
{
return min;
}
else
{
return min.intValue();
}
}
}
public class test {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
String str = sca.nextLine();
String[] strs = str.split(",");
float[] floats = new float[strs.length];
int IntegerNumber = 0;
float sum = 0;
for (int i = 0; i < strs.length; i++) {
floats[i] = Float.parseFloat(strs[i]);
sum += floats[i];
try {
Integer.parseInt(strs[i]);
IntegerNumber++;
} catch (Exception e) {
System.out.println(strs[i]);
}
}
Arrays.sort(floats);
System.out.println("min=" + floats[0] + "\tmax="
+ floats[floats.length - 1] + "\tavg=" + sum / floats.length
+ "\tIntegerNumber=" + IntegerNumber);
}
}
我认为88.0不是整数。。。你需要改的话,自己加个控制
已完成:
public class Test{
/**
* @param args
*/
public static void main(String[] args) {
double [] num = {3,25,2.1,16,14,2,19,91,87,102,0.25,21,75,93,222,15,61,321,1024,525,879,
12,27,3.2,88.0,56,72,1022,128,300,402,505,21.32};
System.out.println("最大值是:"+getMaxValue(num,true));
System.out.println("最小值是:"+getMaxValue(num,false));
System.out.println("个数为:"+num.length);
System.out.println("平均值为"+getAverage(num));
System.out.println("整数个数为:"+getIntNum(num));
}
/**
* 获得整数个数
* @param num
* @return
*/
public static int getIntNum(double [] num){
int index = 0;
for (int i = 0; i < num.length; i++) {
if((""+num[i]).indexOf(".")>0){
index++;
}
}
return index;
}
/**
* 获得平均值
* @param num
* @return
*/
public static double getAverage(double [] num){
double temp = 0 ;
for (int i = 0; i < num.length; i++) {
temp = temp + num[i];
}
return temp/num.length;
}
/**
* 获得最大或最值值(利用冒泡算法)
* @param num 数组
* @param isMaxValue true返回最大值,false返回最小值
* @return
*/
public static double getMaxValue(double [] num,boolean isMaxValue){
if(isMaxValue){
// 定义临时变量
double temp = 0;
// 外循环控制比较的次数
for (int i = 0; i < num.length; i++) {
// 内循环控制比较后移位
for (int j = num.length-1; j > i ; j--) {
if (num[j-1]
num[j-1] = num[j];
num[j] = temp;
}
}
}
return num[0];
}else{
// 定义临时变量
double temp = 0;
// 外循环控制比较的次数
for (int i = 0; i < num.length; i++) {
// 内循环控制比较后移位
for (int j = num.length-1; j > i ; j--) {
if (num[j-1]>num[j]) {
temp = num[j-1];
num[j-1] = num[j];
num[j] = temp;
}
}
}
return num[0];
}
}
}
package cn.com.kerlon;
public class IsNumber {
public static void main(String[] args){
double a[]={3,25,2.1};//这里你按格式把数都放进来
double sum=0;
double max=a[0];
double min=a[0];
double ints=0;
for(int i=0;i
max=a[i];
if(a[i]
sum+=a[i];
if(a[i]%1==0.0)
ints++;
}
System.out.println("最大:"+max+"最小:"+min+"个数 :"+a.length+"平均:"+sum/a.length+"整数个数:"+ints);
}
}