我期望获得最低的运行时间,但它会在arrayList中打印最后一个主菜。我正在尝试为狗打印一个特定的课程,当我输入课程代码时该方法就可以使用。但是,最低的运行时间似乎不起作用。

示例数据

拉兹洛12.2

in 34.2

Yu 23.3-当Lazlo应该是获胜狗时,它将被打印为获胜狗。

获胜的狗应该具有最短的运行时间,但是它取而代之的是读取最后一行并打印。

private void winningDog(String dogsCode)
{
    double runTime = 300;
    String winningDog = "";
    double winningTime = 0;
    for (Dogs dog: dogsList)
    {
        if(runTime > dog.getTotalTime() && (dog.getCourseCode().equalsIgnoreCase(dogsCode)))
        {
            winningTime = dog.getTotalTime();
            winningDog = dog.getName();
        }

    }
    System.out.printf("%n%s%17s%20s%1.2f%n",
          "Winning dog", winningDog,"Time " , winningTime);

}

最佳答案

假设您没有dog.getTotalTime()的负值,则可以更改代码

winningTime = Double.MAX_VALUE; //assigning MAX value to ensure the condition is met at least once(unless all your time values are MAX_VALUE)


然后将您的条件更新为

if(runTime > dog.getTotalTime()..




if(winningTime > dog.getTotalTime().. // this would compare winningTime and you're setting the same is the condition matches

07-26 03:18