0

我的任务是创建一个程序,仅使用输入 N 辆汽车的 for 循环和数学函数,每次迭代都会获得汽车到达终点的时间,最终输出需要是第一个到达终点的输出,并且第二个,意思是两个最低的时间。

我创建了一个程序:

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    System.out.println("enter total cars: ");
    int cars = sc.nextInt();
    double first = 0, second = 0;
    int car1 = 1, car2 = 1;
    for (int i = 1; i <= cars; i++) {
        System.out.println("enter car number " + i + " speed:");
        double speed = sc.nextDouble();
        if (i == 1) {
            first = speed;
            second = speed;
        }   
        
        if (speed < first) {
            second = first;
            first = speed;
            car2 = car1;
            car1 = i;
        } else {
            
            if (speed < second) {
                car2 = i;
                second = speed; 
            }
            
    
        }
    

    }
    System.out.println("car number one is " + car1 +" with speed of " + first);
    System.out.println("car number two is " + car2 +" with speed of " + second);
}

}

我遇到的问题是如何启动第一个和第二个 var,因为它们不能设置为 0,因为我需要检查我得到的速度是否低于第一个或第二个。我可能在第一次迭代中尝试设置第一和第二速度以开始有一些东西,但它仍然存在错误,所以我想知道如何修复它?提前谢谢。

4

2 回答 2

0

所以会是这样的吗?

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("enter total cars: ");
    int totalCars = sc.nextInt();
    
    double temp1 = 0, temp2 = 0, firstPlaceTime = 0, secondPlaceTime = 0;
    int firstPlaceNumber = 0, secondPlaceNumber = 0;
    
   System.out.println("Enter car number 1 time:");
     temp1 = sc.nextDouble();
    System.out.println("Enter car number 2 time:");
    temp2 = sc.nextDouble();
    
    firstPlaceTime = Math.max(temp1, temp2);
    secondPlaceTime = Math.min(temp1, temp2);

    
    for (int i = 3; i <= totalCars; i++) {
        System.out.println("Enter car number " + i + " time:");
         double currentTime = sc.nextDouble();
        
        if (currentTime < firstPlaceTime) {
            secondPlaceTime = firstPlaceTime;
            secondPlaceNumber = firstPlaceNumber;
            firstPlaceTime = currentTime;
            firstPlaceNumber = i;
        }
        else if (currentTime < secondPlaceTime) {
            secondPlaceTime = currentTime;
            secondPlaceNumber = i;
        }
    }
    
    System.out.println("car number one is " + firstPlaceNumber +" with time of " + firstPlaceTime);
    System.out.println("car number two is " + secondPlaceNumber +" with time of " + secondPlaceTime);
}

}

于 2022-03-03T21:30:25.723 回答
0

这是下面的工作代码,它可以让用户输入任意数量的汽车,询问他们的时间,然后打印两个最低时间的车号和他们各自的时间:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("enter total cars: ");
    int totalCars = sc.nextInt();
    
    double firstPlaceTime = Double.MAX_VALUE, secondPlaceTime = Double.MAX_VALUE;
    int firstPlaceNumber = 0, secondPlaceNumber = 0;
    
    for (int i = 1; i <= totalCars; i++) {
        System.out.println("Enter car number " + i + " time:");
        double currentTime = sc.nextDouble();
        
        if (currentTime < firstPlaceTime) {
            secondPlaceTime = firstPlaceTime;
            secondPlaceNumber = firstPlaceNumber;
            firstPlaceTime = currentTime;
            firstPlaceNumber = i;
        }
        else if (currentTime < secondPlaceTime) {
            secondPlaceTime = currentTime;
            secondPlaceNumber = i;
        }
    }
    
    System.out.println("car number one is " + firstPlaceNumber +" with time of " + firstPlaceTime);
    System.out.println("car number two is " + secondPlaceNumber +" with time of " + secondPlaceTime);
}

重要的部分是您可以使用初始化时间doubleDouble.MAX_VALUE以确保前 2 辆汽车将始终替换初始值:

double firstPlaceTime = Double.MAX_VALUE, secondPlaceTime = Double.MAX_VALUE;

除此之外,代码确实具有令人困惑的变量名称。为了使代码更易于理解,我使用了具有更多含义的较长名称。

示例运行:

enter total cars: 
4
Enter car number 1 time:
50
Enter car number 2 time:
30
Enter car number 3 time:
40
Enter car number 4 time:
60
car number one is 2 with time of 30.0
car number two is 3 with time of 40.0
于 2022-03-03T20:51:32.240 回答