Question

In: Computer Science

Java Program. !ONLY USING WHILE LOOPS! (Completed but with errors, wanted to revise could anyone re-create...

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.

Solutions

Expert Solution

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


Related Solutions

Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
In the following Java program replace conditions in while loops to produce Christmas tree output. import...
In the following Java program replace conditions in while loops to produce Christmas tree output. import java.util.Scanner; public class Tree {     public static void main(String[] args)     {         int size;         Scanner scan = new Scanner(System.in);         System.out.print("Enter the size: ");         size = scan.nextInt();         int count = 0;         while (__________) //??? condition         {             int len = 0;             // print blanks             while (____________)//??? condition             {                 System.out.print(' ');                 len++;            ...
Please create using only For loops, as simple as possible as I am attending Java1 Create...
Please create using only For loops, as simple as possible as I am attending Java1 Create two Java programs that do the following: a. Use a for loop to print the numbers below on a single line as shown. 1 2 4 8 16 32 64 128. b. Ask the user to enter numbers using the for loop.  First ask the user how many numbers they will be entering in. Print the sum and average of the entered numbers. c. Write...
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Create a java program that allows people to buy tickets to a concert. Using java create...
Create a java program that allows people to buy tickets to a concert. Using java create a program that asks for the users name, and if they want an adult or teen ticket. As long as the user wants to purchase a ticket the program with "yes" the program will continue. When the user inputs "no" the program will output the customer name, total amount of tickets, and the total price. The adult ticket is $60 and the child ticket...
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Write 2 classes for each program Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix...
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix A; 1 2 3 4 5...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program which can take the input of a decimal number and a numerical base, and convert the decimal number to that base. For example, if given the decimal number seven and the base two, your program should output it as 111, which is how seven is represented in binary. Another example, 8,943 in base 10, is 13,236 in base 9. You’ll need to perform these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT