I remember this is a problem I can run into, but I forget why. Here's my code.
import java.util.Scanner;
public class GroceryTab
{
public static void main(String[] args)
{
double total = 0;
int items = 0;
System.out.print("How many different products are you buying?");
Scanner in = new Scanner(System.in);
items = in.nextInt();
for(int i=1; i<=items; i++) {
double price;
int numberBought;
System.out.print("What is the price of your " + i +"th item?");
Scanner priceIn = new Scanner(System.in);
price = priceIn.nextDouble();
System.out.print("How many of this item are you buying?");
Scanner numIn = new Scanner(System.in);
numberBought = numIn.nextInt();
total += (price * numberBought);
}
System.out.print("Your list costs " + total + " dollars.");
}
}
Here's the weird part. I was testing it out, and I put in the following:
How many different products are you buying?2
What is the price of your 1th item?30.32
How many of this item are you buying?3
What is the price of your 2th item?.01
How many of this item are you buying?3
and got
Your list costs 90.99000000000001 dollars.
Whoops! What did I do to earn this?