【发布时间】:2011-08-05 15:16:09
【问题描述】:
我对我的任务有这个想法,我希望收银机系统在用户输入它的成本价格和所述项目的数量时计算项目的总数。
这似乎可行,但随后导致了我的主要问题 - 我想让用户在说 10 次交易后输入字母“T”,以找出当天的总收入。
我尝试在计算等中使用带有 BigDecimal 数学类的 for 循环。 我的计算中的“valueOf”一词有错误,Eclipse 一直试图将我的值更改为“long”,我很确定这是不对的。
我的解释并不令人惊奇,所以我会给你我写的代码,并将 cmets 放在我的错误所在的旁边..
try{
Scanner in = new Scanner (System.in);
String t = "T";
int count;
for (count = 1;count<=10;count++){
System.out.println("\n\nValue of Item " + count + " :");
BigDecimal itemPrice = in.nextBigDecimal();
System.out.println("Quantity of item " + count + " :");
BigDecimal itemQuantity = in.nextBigDecimal();
BigDecimal itemTotal = (BigDecimal.valueOf(itemPrice).multiply // error here
(BigDecimal.valueOf(itemQuantity))); // error here
System.out.println("\nTotal for item(s): £" + itemTotal);
count++;
while (t == "T"){
BigDecimal amountOfItems = (BigDecimal.valueOf(itemTotal).divide // error here
(BigDecimal.valueOf(itemQuantity))); // error here
BigDecimal totalTakings = (BigDecimal.valueOf(itemTotal).multiply // error here
(BigDecimal.valueOf(amountOfItems))); // error here
System.out.println("The Total Takings For Today is £" + totalTakings + " " );
}
}
}
}
}
就像我说的,Eclipse 用来显示错误的“红线”仅在我的 BigDecimal 计算中的“valueOf”字样下方。
任何帮助都会很棒,因为我正在撕扯我的头发!!!!
谢谢,
维尼。
【问题讨论】:
-
注意:永远不要将 Java 字符串与
==进行比较,因为字符串 不是 原语,它们是对象 - 因此==是引用比较,而不是相等比较.请改用.equals()(或.equalsIgnoreCase())方法,因为这将进行您期望的比较。
标签: java while-loop bigdecimal value-of