/*
08-Java语言基础(数组常见操作-获取最值)
*/
import java.lang.*;
class ExtremeValueArray
{
public static void main(String args[])
{
int x[] = new int[128];
int power = getPower(x.length);
assignmentArray(x);
printArray(x);
System.out.println("Power:"+power);
System.out.println("MaxValue:"+getMaxValue(x));
System.out.println("MinValue:"+getMinValue(x));
System.out.println("AvgValue:"+getAvgValue(x,power));
}
public static int getMaxValue(int x[])
{
int max = x[0];
for(int i = 0;i<x.length;i++)
{
if(x[i]>max)
{
max = x[i];
}
}
return max;
}
public static int getMinValue(int x[])
{
int min = x[0];
for(int i = 0;i<x.length;i++)
{
if(x[i]<min)
{
min = x[i];
}
}
return min;
}
public static int getAvgValue(int x[],int power)
{
int avg = 0;
int sum = 0;
for(int i = 0;i<x.length;i++)
{
sum += x[i];
}
System.out.println("Sum:"+sum);
avg = sum >>> power;
return avg;
}
public static void printArray(int x[])
{
int count = 0;
for(int i = 0;i<x.length;i++)
{
++count;
if(i!=x.length-1)
System.out.println(x[i]+"\t");
else
{
System.out.print(x[i]+"\t");
System.out.println("count:"+count);
}
}
}
public static void assignmentArray(int x[])
{
for(int i = 0;i<x.length;i++)//0-9
{
x[i] = i<<x.length; //左移动128位为什么会打印出来1、2、3、4、5... ...?
}
}
public static int getPower(double x){
double i = 2;
int power = (int)(Math.log(x)/Math.log(i));
return power;
}
}
public static void assignmentArray(int x[])
{
for(int i = 0;i<x.length;i++)//0-9
{
x[i] = i<<x.length; //左移动128位为什么会打印出来1、2、3、4、5... ...?
}
} |
|