i have a coding problem with a loop task, more info below

107 views Asked by At

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.

2

There are 2 answers

12
Nexevis On

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
0
fun On

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

}