我的循环任务存在编码问题,更多信息如下

发布于 2025-01-11 16:06:40 字数 1222 浏览 0 评论 0原文

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

我创建了一个程序:

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

}

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

I have a task to create a program using only for loop and math functions that input N amount of cars and each iteration gets the time it took the car to reach the end, the final output needs to be the first one to reach the end and the second, meaning the two lowest times.

I've created a program:

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

}

the problem I had is how to start the first and second var, as they cant be set to 0, because I need to check if the speed I get is lower than the first or the second. i tried maybe on the first iteration to set first and second to speed to have something to start with, but it still bugs, so I wondered how could I fix it?
thanks ahead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2025-01-18 16:06:40

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

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

重要的部分是您可以初始化时间使用 Double.MAX_VALUEdouble 值来确保前 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

Here is the working code below that will let the user enter any number of cars, ask for their time taken, and then print the car number of the two lowest times and their respective time taken:

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

The important part is that you can initialize the time double values using Double.MAX_VALUE in order to ensure the first 2 cars will always replace the initial values:

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

Besides that the code honestly has the confusing variable names. I used longer names that have more meaning in order to make the code easier to understand.

Example Run:

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
爱要勇敢去追 2025-01-18 16:06:40

那么会是这样的事情吗?

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

}

So it will be something like this?

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文