Java 如何在此代码中正确编码最大值、最小值和平均值?
我不明白为什么我的代码是:Java 如何在此代码中正确编码最大值、最小值和平均值?,java,if-statement,null,Java,If Statement,Null,我不明白为什么我的代码是: 给我一个空输出 不计算最大值、最小值和平均值 我的代码中有两个类,我应该找到一组值的平均值、最大值和最小值 公共类循环v8 { //私有实例变量的声明 私人双myRadius,mySphere; 私人双myArea=0,合计=0; //总计平均值的实例变量 私人双MyTotal=0; 私人双平均=0; //CirclesV8类型对象的构造函数 公共电路V8(整数r,整数s) { myRadius=r; mySphere=s; MyTotal=总计; } //半径和总
公共类循环v8
{
//私有实例变量的声明
私人双myRadius,mySphere;
私人双myArea=0,合计=0;
//总计平均值的实例变量
私人双MyTotal=0;
私人双平均=0;
//CirclesV8类型对象的构造函数
公共电路V8(整数r,整数s)
{
myRadius=r;
mySphere=s;
MyTotal=总计;
}
//半径和总半径的getter方法
公共双getRadius()
{
返回myRadius;
}
公共双getSphere()
{
回归我的世界;
}
公共双getTotal()
{
返回MyTotal;
}
//面积、平均值和周长的变数法
公共行政区()
{
myArea=Math.PI*(Math.pow(myRadius,2));
}
公共区域()
{
返回myArea;
}
公共空隙calcSurfaceArea()
{
mySphere=(Math.pow(myRadius,2))*Math.PI*4;
}
公共双getsurfaceearea()
{
回归我的世界;
}
公共空间(vg)
{
MyAverage=MyTotal/5;
}
公共双getAvg()
{
返回我的平均值;
}
//myTotal的setter方法
公共空间集合总计(双倍总计)
{
MyTotal=总计;
}
//返回对象值的字符串。format()方法类似于printf()。
公共字符串toString()
{
返回字符串.format(“%12d%14.1f%13.1f 12.1f”),myRadius,
myArea,
mySphere,
我的平均数);
}
}
导入java.util.Scanner;
公共类V8Tester
{
公共静态void main(字符串[]args)
{
//创建扫描仪对象
扫描仪输入=新扫描仪(系统输入);
//初始化变量
内半径1,半径2,半径3,半径4,半径5;
int sphere1、sphere2、sphere3、sphere4、sphere5;
双平均=0;
双倍合计=0;
双最小值=整数最大值;
double max=整数.MIN_值;
System.out.println(“请输入五个半径,以空格分隔(例如6 12)”;
半径1=in.nextInt();
半径2=in.nextInt();
半径3=in.nextInt();
半径4=in.nextInt();
半径5=in.nextInt();
System.out.println(“请输入另外5个半径,按空格分隔(例如6 12)”;
sphere1=in.nextInt();
sphere2=in.nextInt();
sphere3=in.nextInt();
sphere4=in.nextInt();
sphere5=in.nextInt();
//创建对象数组
CirclesV8[]圆={新的CirclesV8(半径1,球体1),
新圆圈V8(半径2,球体2),
新圆圈V8(半径3,球体3),
新圆圈V8(半径4,球体4),
新的圆圈V8(半径5,球体5)};
//调用方法
对于(int index=0;indexmax)
max=circles[index].getTotal();
}
//设置总值&调用average方法以使用其中一个对象计算平均值
圆圈[4]。设置总计(总计);
圆[4]。钙质();
圆[4]。calcSurfaceArea();
圆[4]。getAvg();
//圆圈颜色
System.out.println(“圆的颜色#1:”);
字符串circle1=in.next();
System.out.println(“圆的颜色#2:”);
字符串circle2=in.next();
System.out.println(“圆的颜色#3:”);
字符串circle3=in.next();
System.out.println(“圆的颜色#4:”);
字符串circle4=in.next();
System.out.println(“圆的颜色#5:”);
字符串circle55=in.next();
String[]数组=新字符串[5];
数组[0]=circle1;
数组[1]=圆圈2;
数组[2]=圆圈3;
数组[3]=圆圈4;
数组[4]=圈55;
//球体颜色
System.out.println(“球体的颜色#1:”);
字符串sphere11=in.next();
System.out.println(“球体的颜色#2:);
字符串sphere22=in.next();
System.out.println(“球体的颜色#3:);
字符串sphere33=in.next();
System.out.println(“球体的颜色#4:);
字符串sphere44=in.next();
System.out.println(“球体的颜色#5:”);
字符串sphere55=in.next();
字符串[]arr=新字符串[5];
数组[0]=sphere11;
数组[1]=sphere22;
数组[2]=sphere33;
数组[3]=sphere44;
数组[4]=sphere55;
int i=1;
System.out.println(“颜色半径表面积”);
System.out.println(“=====================================================================================================================================”);
对于(int index=0;index for(int index = 0; index < circles.length; index++)
{
System.out.printf("%4s %10.1f %20f", arr[index],
circles[index].getRadius(),
circles[index].getSurfaceArea(),
i++);
}
System.out.println("==============================================================");
System.out.printf("%20s%n", "Minimum: ", max);
System.out.printf("%20s%n", "Maximum: ", min);
System.out.printf("%20s%n", "Average: ", circles[4].getAvg());
}
}