1

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!

Yousha Arif
  • 1,188
  • 8
  • 20
Sk8rTalent
  • 11
  • 1

2 Answers2

0

You can typecast the double value to int and then use this in some if statement to see if the value is < 10 or not depending on that do further operation.

Sample code :

 double total = 0.0;
 double avg = 0.0;
 double b = 0.0;
 Scanner cs = new Scanner(System.in);

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

 double a = cs.nextDouble();
 int value = (int) a;
 int i = 0;
 if (value < 10) {
  //loop through items
  for (i = 0; i < value; i++) {

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

   b = cs.nextDouble();
   //add to total
   total += b;

  }
  //get avg with total no and total quantity
  avg = (double)((total / value));
  System.out.println("Your total is: " + total + "\nThe average of your purchase is: " + avg);
 } else {

  System.out.println("Incorrect Entry! You can only buy up to 10 items at once!");


 }

Output :

What is the quantity of your purchase?                                                                                        
13                                                                                                                            
Incorrect Entry! You can only buy up to 10 items at once!
Swati
  • 28,069
  • 4
  • 21
  • 41
  • Thats what I was missing, every time I got a successful loop I would lose my value. Thanks! Now I just gotta figure out how to loop the whole things four times and add all four together. – Sk8rTalent Jul 06 '20 at 04:37
  • just add a loop inside if statement i.e :`for(i=0;i – Swati Jul 06 '20 at 04:40
  • You can set this answer as [accepted](https://stackoverflow.com/help/someone-answers).. if that solve your problem – Swati Jul 07 '20 at 04:17
0

You can just make the quantity variable integer as the quantity will always be a decimal number and insert an if statement

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?");
    int qunatity = cs.nextInt();
    if(a > 10) {
        System.out.println("Incorrect Entry! You can only buy up to 10 items at once!");
    }
    System.out.println("How much does your purchase cost?");
    double cost = cs.nextDouble();
    // System.out.println("Would you like to purchase anything else? Please type yes or no.");
    double total = a*b;
    avg = total / quantity;
    System.out.println("Your total is: "+total"\nThe average of your purchase is: " + avg);

  }

Nishith Savla
  • 310
  • 2
  • 10