嵌套if语句的逻辑错误

时间:2013-01-11 04:36:42

标签: java if-statement nested

我正在尝试创建一个提供比赛前5名的程序。代码编译,但是当你运行它时会出现明显的逻辑错误。它在所有5个地方重复相同的决赛选手,所以第一名被放置在第一名,第二名,第三名等等......

这是我的代码:

import java.util.Scanner;

public class Assignment0
{
    public static void main (String [] args)
    {
        int numberOfLanes;
        int lane = 0;
        double first;
        double second;
        double third;
        double fourth;
        double fifth;
        double [] time = null;

        Scanner keyboard = new Scanner (System.in);

        System.out.println ("How many lanes hold competitors?");
        numberOfLanes = keyboard.nextInt();

        time = new double [numberOfLanes];
        for (int i = 0; i < numberOfLanes; i++)
        {
            System.out.println("Enter time for lane " + i);
            time [i] = keyboard.nextDouble();
        }

        System.out.println();
        System.out.println();

         first = time[0];

         for (int i = 0; i < time.length; i++)
         {
             if ( time[i] < first)
                {
                    first = time[i];
                    lane = i;
                }
         }

         System.out.println("First place = Lane " + lane + ". Time = " + first + " seconds.");

         second = time[0];

         for (int i = 0; i < time.length; i++)
         {
             if (time [i] > first)
                if (time [i] < second)
                {
                     second = time[i];
                     lane = i;
                } 
         }

         System.out.println("Second place = Lane " + lane + ". Time = " + second + " seconds.");

         third = time[0];

         for (int i = 0; i < time.length; i++)
         {
             if (time[i] > second)
                if (time[i] < third)
                {
                    third = time[i];
                    lane = i;
                }

         }

         System.out.println("Third place = Lane " + lane + ". Time = " + third + " seconds.");

         fourth = time[0];

         for (int i = 0; i < time.length; i++)
         {
             if (time[i] > third)
                if (time[i] < fourth)
                            {    
                              fourth = time[i];
                              lane = i;
                            }
         }


         System.out.println("Fourth place = Lane " + lane + ". Time = " + fourth + " seconds.");

         fifth = time[0];

         for (int i = 0; i < time.length; i++)
         {
           if (time [i] > fourth)
            if (time[i] < fifth)
              {
                fifth = time[i];
                lane = i;
              }
         }


         System.out.println("Fifth place = Lane " + lane + ". Time = " + fifth + " seconds.");
    }
}

输出如下:

How many lanes hold competitors?
5
Enter time for lane 0
9.72
Enter time for lane 1
9.8
Enter time for lane 2
9.82
Enter time for lane 3
9.86
Enter time for lane 4
9.9


First place = Lane 0. Time = 9.72 seconds.
Second place = Lane 0. Time = 9.72 seconds.
Third place = Lane 0. Time = 9.72 seconds.
Fourth place = Lane 0. Time = 9.72 seconds.
Fifth place = Lane 0. Time = 9.72 seconds.

7 个答案:

答案 0 :(得分:1)

您在每个循环之前设置second = time[0];third = time[0]等,因此在每个for循环中,if语句条件永远不会为真,因此变量second,{ {1}}等永远不会从third更改而time[0]永远不会从lane更改。

您应该使用0初始化它们。具体而言,在声明时,请使用以下代码:

Double.MAX_VALUE

并删除所有 int numberOfLanes; int lane = 0; double first = Double.MAX_VALUE; double second = Double.MAX_VALUE; double third = Double.MAX_VALUE; double fourth = Double.MAX_VALUE; double fifth = Double.MAX_VALUE; double [] time = null; second = time[0];,依此类推。

答案 1 :(得分:0)

您的初始化很奇怪,而不是设置first = time[0]等,只需将它们初始化为无穷大并删除单独的分配行。

double first = Double.POSITIVE_INFINITY;
double second = Double.POSITIVE_INFINITY;
double third = Double.POSITIVE_INFINITY;
double fourth = Double.POSITIVE_INFINITY;
double fifth = Double.POSITIVE_INFINITY;

答案 2 :(得分:0)

first = time[0];

,直到

fifth = time[0];

答案 3 :(得分:0)

second = time[0];更改为second = time[1];

third = time[0];third = time[2];

fourth = time[0];fourth = time[3];

fifth = time[0];fifth = time[4];

答案 4 :(得分:0)

首先,这不是解决问题的好方法。但是,如果我们坚持使用此方法,则解决方案是初始化secondthirdfourthfifth,如下所示:

second = Double.MAX_VALUE;

依此类推第3至第5名。您还需要将其作为代码的头部:

import java.lang.Double;

但请注意,即使使用上述修复,如果两个通道的时间相同,则会跳过第二个通道,最后一个通道的时间为MAX_VALUE,因此您需要完全重写代码才能使用数组并对它们进行排序。

答案 5 :(得分:0)

您在比较时间的循环之前无条件地分配second和所有其他排名。因此,如果将最快的时间作为元素[0]放置,您将获得观察到的行为。您应该阅读sort

答案 6 :(得分:0)

怎么样:

    String[] data = new String[]{"First","Second","Third", "Forth", "Fifth"};
    int num[] = new int[]{0,1,2,3,4};
    //now we sort the array(time) and we note the index
    for(int a=0;a<numberOfLanes;a++){
        for(int b=a+1;b<numberOfLanes;b++){
            if(time[a]>time[b]){
                int temp=time[a];
                time[a] = time[b];
                time[b] = temp;

                temp = num[a];
                num[a] = num[b];
                num[b] = temp;
            }
        }
    }
    //--
    //print the output
    for(int a=0;a<numberOfLanes;a++){
        System.out.println(data[a]+" place = Lane "+num[a]+". Time = "+time[a]+" seconds.");
    }
    //-- done