为什么购买scan.next()
是不兼容的类型?
(我正在尝试获取用户输入以进行购买和招标,然后使用方法计算更改)。
public static void makeChange() //one method of a class
{
double purchase;
double tendered;
Scanner scan = new Scanner (System.in);
System.out.println ("How much was the Purchase?");
purchase = scan.next(); //why would this be an incompatible type?
System.out.println ("Amount Tendered");
tendered = scan.next();
System.out.println("Processing Transaction");
int ch[] = cd.makeChange(purchase, tendered);
.... continued
答案 0 :(得分:5)
因为scanner.next()返回javadoc中描述的字符串。这是第一个检查方法何时不能按照您的想法进行检查的地方。您可能打算使用scanner.nextDouble()
。
正如评论中所指出的,如果你的代码不仅仅是一个练习,你应该使用BigDecimal来获取金额以避免舍入问题(双精度不准确)。