我记得这是我可能遇到的问题,但我忘记了原因。这是我的代码。
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.");
}
}
这是奇怪的部分。我正在对其进行测试,然后输入以下内容:
您购买了多少种不同的产品?2
您的第 1 件商品的价格是多少?30.32
您要购买多少件此商品?3
您的第 2 件商品的价格是多少?.01
您要购买多少件此商品?3
并得到
您的清单花费 90.99000000000001 美元。
哎呀!我做了什么来赚这个?