0

我正在尝试创建一个程序来展示比赛中的前 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.
4

7 回答 7

1

您在每个循环之前设置second = time[0];,third = time[0]等,因此在每个for循环中,if 语句条件永远不会为真,因此变量second,third等永远不会改变,time[0]lane永远不会改变 from 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];, third = time[0], 等等。

于 2013-01-11T04:43:14.390 回答
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;
于 2013-01-11T04:43:47.800 回答
0
first = time[0];

直到

fifth = time[0];
于 2013-01-11T04:44:15.343 回答
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];

于 2013-01-11T04:44:23.727 回答
0

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

second = Double.MAX_VALUE;

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

import java.lang.Double;

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

于 2013-01-11T04:45:06.857 回答
0

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

于 2013-01-11T04:45:48.730 回答
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
于 2013-01-11T12:17:06.660 回答