【问题标题】:How do I find the minimum and maximum using Integer.MAX_VALUE?如何使用 Integer.MAX_VALUE 找到最小值和最大值?
【发布时间】:2017-04-22 20:50:08
【问题描述】:

我很困惑,一直纠结于如何在数组中找到我的代码的最大值和最小值。我必须使用 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);
}
}

这里和我的其他类一起使用

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;
}
}

我要求简要介绍如何找到最小值和最大值是数组。尤其是相同的特定数字(例如 51,106 是我的距离)

【问题讨论】:

    标签: java arrays integer max min


    【解决方案1】:

    恐怕我必须猜测您希望最小化或最大化什么值。所以不管是距离,因为你想使用一个整数值。

    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();
        .... 
    
    }
    

    如果您使用双精度值,请注意 Double.MIN_VALUE 是双精度可能具有的最小值。您将需要 -Double.MAX_VALUE。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多