In: Computer Science
Java Program. !ONLY USING WHILE LOOPS!
(Completed but with errors, wanted to revise could anyone re-create this?)
Write a program to keep track of the number of miles you have driven during an extended vacation and the number of gallons of gasoline you used during this time, recorded at weekly intervals. This vacation will last over several weeks (the precise number of weeks while making the program is unknown to the coder). Ask the user to enter the number of miles driven afterward ask them how many gallons of gasoline they purchased for each week of the vacation. If the user enters -99 when asked for the number of miles driven, the vacation is over and the program will end by printing "Vacation Over!". Express all numeric values rounded to the nearest hundredth.
The task is to create this in Java using only while loops.
Thank you.
import java.util.Scanner;
public class VocationDrive {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
double totalMiles = 0;
double totalGas = 0;
int totalWeeks = 0;
double n = 0;
while (true) {
System.out.println("Enter the miles driven: ");
n =
sc.nextDouble();
if (n ==
-99)
break;
totalWeeks++;
totalMiles +=
n;
System.out.println("Enter total gallons gas: ");
n =
sc.nextDouble();
totalGas +=
n;
}
System.out.println("Vacation
Over!");
System.out.println("Total miles: "
+ totalMiles);
System.out.println("Total gas: " +
totalGas);
System.out.println("Total weeks: "
+ totalWeeks);
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me