Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how I would answer it. Like @IswantoSan said, <code>NumberFormat.format()</code> does not compile so I changed that. Also, I got rid of all of the re-initialization that occurs every time you go through the loop. It is unneeded. Doesn't take up that much more memory but I was always told it is good practice. Also, the first letter in variable names should be lower case. Again, I was always told that is good practice. I could be wrong.</p> <p>I hope this is what you were looking for.</p> <pre><code>final static double espressoPrice = 2.00; final static double cappuccinoPrice = 3.25; final static double lattePrice = 3.50; final static double coffeePrice = 1.50; public static void main(String[] args) { int customersSimulated = 0; int maxCustomersSimulated = 4; int randomQuantity = 0; double total = 0; double totalCoffee = 0; double totalCappuccino = 0; double totalLatte = 0; double totalEspresso = 0; DecimalFormat df = new DecimalFormat("##.##"); while (customersSimulated &lt;= maxCustomersSimulated) { System.out.println("Customer " + customersSimulated); Random randomList = new Random(); int randomItem = randomList.nextInt(4) + 1; if (randomItem == 1) { System.out.println("Item purchased: Coffee"); randomQuantity = randomList.nextInt(5) + 1; System.out.println("Quantity purchased: " + randomQuantity); double totalCoffeeCost = randomQuantity * coffeePrice; System.out.println("Total Cost: $" + df.format(totalCoffeeCost)); totalCoffee+=totalCoffeeCost; } else if (randomItem == 2) { System.out.println("Item purchased: Latte"); randomQuantity = randomList.nextInt(5) + 1; System.out.println("Quantity purchased: " + randomQuantity); double totalLatteCost = randomQuantity * lattePrice; System.out.println("Total Cost: $" + df.format(totalLatteCost)); totalLatte+=totalLatteCost; } else if (randomItem == 3) { System.out.println("Item purchased: Cappuccino"); randomQuantity = randomList.nextInt(5) + 1; System.out.println("Quantity purchased: " + randomQuantity); double totalCappuccinoCost = randomQuantity * cappuccinoPrice; System.out.println("Total Cost: $" + df.format(totalCappuccinoCost)); totalCappuccino+=totalCappuccinoCost; } else if (randomItem == 4) { System.out.println("Item purchased: Espresso"); randomQuantity = randomList.nextInt(5) + 1; System.out.println("Quantity purchased: " + randomQuantity); double totalEspressoCost = randomQuantity * espressoPrice; System.out.println("Total Cost: $" + df.format(totalEspressoCost)); totalEspresso+=totalEspressoCost; } System.out.println(); System.out.println("Total Coffee Cost: $" + totalCoffee); System.out.println("Total Cappuccino Cost: $" + totalCappuccino); System.out.println("Total Llatte Cost: $" + totalLatte); System.out.println("Total Espresso Cost: $" + totalEspresso); System.out.println(); customersSimulated++; } } </code></pre> <p>Basically, when you initialize and define a variable outside that while loop, <code>+=</code> it within the loop, it can be accessed outside the loop and will be "remembered" throughout the code being ran; if that makes sense to you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload