0

なぜ購入は互換性のないタイプになるの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
4

1 に答える 1

5

Scanner.next()は、javadocで説明されている文字列を返すためです。これは、メソッドが想定どおりに機能しない場合に最初に確認する場所です。あなたはおそらくを使用するつもりscanner.nextDouble()でした。

コメントで指摘されているように、コードが演習以上のものである場合は、丸めの問題を回避するために、金額にBigDecimalを使用する必要があります(倍精度は正確ではありません)。

于 2012-07-24T07:36:14.423 に答える