简体   繁体   中英

How do I convert my while loop to a for loop?

I am a beginner coder using Netbeans Java. I have created a code that initially asks how many gallons are in your gas tank. Then, it will have a while loop asking how many miles you will be traveling for this first run and how fast are you traveling. This will repeat with a while loop until you input '0' to stop adding trips. I am stumped on how to convert this while loop into only using For loops. I would greatly appreciate the assistance. Here is my code that has while loops.

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
            int tank;
            double miles;
            double speed;
            double totalMiles = 0.0;
            int choice;
            double time;
            double totalTime = 0.0;
            double fuelConsumption;

            System.out.print("How many gallons of gas is in your tank (Integer 1-15)? ");
            tank = input.nextInt();
            System.out.printf("%s%d%s\n\n" , "You have ", tank , " gallons of gas in your tank.");

            System.out.print("Are you going on a trip (1 = Yes or 0 = No)? ");
            choice = input.nextInt();

            while (choice == 1)
            {
                System.out.print("How many miles are you traveling? ");     // miles 
                miles = input.nextFloat();

                System.out.print("What is your speed for this run (MPH)? ");      // speed
                speed = input.nextInt();
                System.out.print("\n");


                totalMiles = totalMiles + miles;
                time = (miles/speed);
                totalTime += (time*60);
                fuelConsumption = (20*(tank/totalMiles));

                System.out.print("Is there another leg in your trip (1 = Yes or 0 = No)? ");    // asking another leg
                choice = input.nextInt();

                if (choice == 0)
                {
                    System.out.printf("%s%5.2f%s\n","Your data for this trip is: \n"
                            + "You traveled a total of about ", totalMiles , " miles.");
                    System.out.printf("%s%.2f%s\n" , "You traveled about " , totalTime , " minutes.");

                    if (fuelConsumption >= 2)
                    {
                        System.out.println("Your car has enough gas to return.");
                        break;

                    }
                    else
                    {
                        System.out.println("Your car will need more gas to return.");
                        break;
                    }


                }
            }

}

}

That is not a use case for a for loop, where we iterate over a known number of elements for do a known number of iterations. Like, repeat 10 times or such.

Technically it can be solved with a for loop, but that is abusing the concept a bit. The while loop is a perfect fit for that task.

This is not a place to use a for-loop, you use a for loop for something like this:


printAmount = 10;

for (int i = 0; i < printAmount; i++) {

   System.out.println("Hello World"); 

}

Here you are using the for loop to print "Hi" for the amount in printAmount.

Your case is different: You want the while-loop to repeat while the input is "1" so you use a WHILE-loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM