CAN U FIX MY CODE HERES THE QUESTION Write a program to proc

CAN U FIX MY CODE?

HERE\'S THE QUESTION.

Write a program to process items that a user purchases. i. We like to be able to have the user enter prices for as many items that she wants to purchase. Allow for a sentinel value to end the input loop. ii. Do not allow the user to enter an invalid price (A number greater than 1000). Issue an error message and allow the user to enter a valid price. iii. Calculate and display the total amount of all purchases and the number of items purchased. iv. Calculate and display the total amount with an applied 8% tax rate. v. Calculate and display the average price of all purchases after the application of tax.

HERE\'S THE MAIN OF THE CODE:

//variable definitions
  
double price;
double total = 0;
double totalCount = 0;
double total_incTax = 0;
double average = 0;
  
while(true){
  
System.out.println(\"Enter the price of the item or -1 to quit\");
price = input.nextDouble();
  
total = total + price;
totalCount = totalCount +1;
total_incTax = total + (0.08 *total);
average = total / totalCount;
  
if(price == -1){
break;
}
if (price <= 1000) {
System.out.println(\"Total price is\" + total);
System.out.println(\"Total price including tax is\" + total_incTax);
System.out.println(\"average is\" + average);
price++;   
}
  
else{
System.out.println(\"Invalid price. Please enter a valid price\");
  
THE CODE CALCULATES THE TOTAL AND AVERAGE FOR EVERY SINGLE NUMBER ENTERED. INSTEAD OF CALCULATING FOR ALL OF IT??

Solution

import java.util.Scanner;
public class Test{
   public static void main(String args[]){
       //variable definitions

       double price;
       double total = 0;
       double totalCount = 0;
       double total_incTax = 0;
       double average = 0;

       // input was not defined
       Scanner input = new Scanner(System.in);

       while(true){

           System.out.print(\"Enter the price of the item or -1 to quit: \");
           price = input.nextDouble();
           if(price == -1){
               break;
           }
          
           if (price <= 1000) {
               total = total + price;
               totalCount = totalCount + 1;
               price++;   
           }

           else{
               System.out.println(\"Invalid price. Please enter a valid price\");
           }
       }
      
       total_incTax = total + (0.08 * total);
       average = total / totalCount;

       System.out.println(\"Total price is \" + total);
       System.out.println(\"Total price including tax is \" + total_incTax);
       System.out.println(\"average is \" + average);
   }
}

CAN U FIX MY CODE? HERE\'S THE QUESTION. Write a program to process items that a user purchases. i. We like to be able to have the user enter prices for as many
CAN U FIX MY CODE? HERE\'S THE QUESTION. Write a program to process items that a user purchases. i. We like to be able to have the user enter prices for as many

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site