[英]How do I find the minimum and maximum using Integer.MAX_VALUE?
I am confused and stuck on how to find the max and minimum of my code in the array. 我对如何在数组中查找代码的最大值和最小值感到困惑和困惑。 I have to use Integer.MAX_Value and Integer.MIN_Value.
我必须使用Integer.MAX_Value和Integer.MIN_Value。
public class AnnualFuelUseTester
{
public static void main(String [] args)
{
int sMiles1, eMiles1, dist;
double price , gallons, mpg, GPM;
int counter = 0;
int total;
AnnualFuelUse[] annual = {new AnnualFuelUse(12700,12751,51,2.8),
new AnnualFuelUse(12802,12908,106,5.8),
new AnnualFuelUse(12908,13014,106,5.8)};
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int index = 0; index < annual.length; index++)
{
annual[index].calcDistance();
annual[index].calcMPG();
annual[index].calcTotal();
}
System.out.println(
"Fill up Start Miles End Miles Distance Gallons Used MPG Price Cost");
for(int index = 0; index < annual.length; index++)
{
System.out.printf(" %d %6d %5d %5d %.2f %.2f %.2f %.2f\n",
index + 1,
annual[index].getStart(),annual[index].getEnd(),
annual[index].getDist(), annual[index].getGal(),
annual[index].getMPG(), annual[index].getPrice(),
annual[index].getTotal());
}
System.out.println("Minimum: " + max);
}
}
And here my other class used together 这是我其他班级一起使用的
public class AnnualFuelUse
{
private int sMiles1, eMiles1,dist;
private double price, gallons, mpg, total, GPM;
AnnualFuelUse(int s1, int e1, int distance, double gals)
{
sMiles1 = s1;
eMiles1 = e1;
dist = distance;
price = 1.83;
gallons = gals;
}
public void calcDistance()
{
dist = eMiles1 - sMiles1;
}
public double getPrice()
{
return price;
}
public void calcMPG()
{
mpg = dist / gallons;
}
public int getStart()
{
return sMiles1;
}
public int getEnd()
{
return eMiles1;
}
public int getDist()
{
return dist;
}
public double getMPG()
{
return mpg;
}
public double getGal()
{
return gallons;
}
public void calcTotal()
{
total = 1.83 * gallons;
}
public double getTotal()
{
return total;
}
}
I am asking for a brief run over on how to find the minimum and maximum is arrays. 我要求简要介绍如何找到最小和最大数组。 Especially specific numbers that are the same (eg 51,106 is my distances)
尤其是相同的特定数字(例如,我的距离是51,106)
I'm afraid I have to guess what value you would like to minimize or maximize. 恐怕我不得不猜测您想要最小化或最大化的价值。 So be it the distance, because you want to use an Integer value.
就是距离,因为您要使用Integer值。
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int index = 0; index < annual.length; index++)
{
annual[index].calcDistance();
if (annual[index].getDist() < min) annual[index].getDist() = min;
if (annual[index].getDist() > max) annual[index].getDist() = max;
annual[index].calcMPG();
annual[index].calcTotal();
....
}
If you use a double value, be aware that Double.MIN_VALUE is the mnallest value a double could have. 如果您使用double值,请注意Double.MIN_VALUE是double可能拥有的最大的值。 You would need -Double.MAX_VALUE instead.
您将需要-Double.MAX_VALUE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.