dimanche 5 juillet 2020

Beginner Java problem regarding storing totals and finding the average

I am doing my first coding course and I have run into an issue with my loops. I am supposed to write a program that takes the user input of a specific quantity and specific price and multiplies them together, and finds the averages of my purchase. The problem I've run into is that you cannot have over a quantity of 10 items total and I am at a loss as to how to make sure the qunatity doesnt end up over 10 by the time I multiply and average I have tried if then loops that ultimately either skip over to the next line or remove the initial value when asked if they would like to purchase anything else I will post my code below as well as a sample output of what we are supposed to end up with, thanks for any help or advice for a beginner!

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner cs=new Scanner(System.in);

System.out.println("What is the quantity of your purchase?");

double a = cs.nextDouble();


  }


System.out.println("How much does your purchase cost?");




double b = cs.nextDouble();

  System.out.println("Would you like to purchase anything else? Please type yes or no.");


double total = a*b;

avg = (Float) ((a+b/2);

System.out.println("Your total is: "+total"\nThe average of your purchase is: " + avg);

  }

}

Sample Output:

Enter Quantity: 3 Enter Price: 8.50 This will cost $25.50

Enter Quantity: 12 Incorrect Entry! You can only buy up to 10 items at once!

Enter Quantity: 2 Enter Price: 5.50 This will cost $11.00

Enter Quantity: 4 Enter Price: 3.00 This will cost $12.00

You have ordered 9 items costing $48.50, averaging $5.39 each

It doesnt have to come out looking exactly like this but the general idea is ordering various not described products that come out to a quantity and price total, and end up being averaged. The only rule is that the quantity must remain under 10 or under and I am using floating variables or double. Thanks again!

Aucun commentaire:

Enregistrer un commentaire