0

変数を含む数式の簡単な数学計算を実行しようとしました。ただし、変数の型が一致しないことを示すエラーがコンパイラに表示されます。変数の型をキャストして変更しようとしましたが、うまくいきません。コードの基本的な形式を破壊せずにこれを修正するにはどうすればよいですか。

私はJavaの経験がないので、どんなポインタでも役に立ちます。

これがコードです。お金の変換プログラム用です。エラーは、コードの 2 番目の部分、else 部分にあります。

import java.util.Scanner;
public class MConvert
{
    public static void main (String[] args)
   {
     int penny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar,     twentyDollar, fiftyDollar, hundredDollar; 
     //can sub $ sign for dollar in variable, convention otherwise
     double totalMoney; 
     Scanner scan = new Scanner (System.in);
        System.out.println ("Are you converting to the total? If so, type true. \nIf you are converting from the total, then type false.");
       boolean TotalorNot = true;
        TotalorNot = scan.nextBoolean();


       if (TotalorNot) {

        System.out.println ("Type in the number of one-hundred dollar bills.");
        hundredDollar = scan.nextInt();
        System.out.println ("Type in the number of fifty dollar bills.");
        fiftyDollar = scan.nextInt();
        System.out.println ("Type in the number of twenty dollar bills.");
        twentyDollar = scan.nextInt();
        System.out.println ("Type in the number of ten dollar bills.");
        tenDollar = scan.nextInt();
        System.out.println ("Type in the number of five dollar bills.");
        fiveDollar = scan.nextInt();
        System.out.println ("Type in the number of one dollar bills or coins.");
        dollar = scan.nextInt();
        System.out.println ("Type in the number of half-dollar coins.");
        halfDollar = scan.nextInt();
        System.out.println ("Type in the number of quarter-dollar coins.");
        quarter = scan.nextInt();
        System.out.println ("Type in the number of dimes.");
        dime = scan.nextInt();
        System.out.println ("Type in the number of nickels.");
        nickel = scan.nextInt();
        System.out.println ("Type in the number of pennies coins.");
        penny = scan.nextInt();
        totalMoney = (hundredDollar * 100) + (fiftyDollar * 50) + (twentyDollar * 20) + (tenDollar * 10) + (fiveDollar * 5) + (dollar * 1) + ((double)halfDollar * 0.5) + ((double)quarter * 0.25) + ((double)dime * 0.1) + ((double)nickel * 0.05) + ((double)penny * 0.01); 
    System.out.println ("Here is total monetary value of the bills and coins you entered: " + totalMoney);




}    else {

        System.out.println ("Type in the total monetary value:");
        totalMoney = scan.nextDouble();
        hundredDollar = ((int)totalMoney / 100);
        fiftyDollar = ((int)totalMoney - (hundredDollar * 100)) / 50;
        twentyDollar = ((int)totalMoney - (fiftyDollar * 50)) / 20;
        tenDollar = ((int)totalMoney - (twentyDollar * 20)) / 10;
        fiveDollar = ((int)totalMoney - (tenDollar * 10)) / 5;
        dollar = ((int)totalMoney - (fiveDollar * 5)) / 1;
        (double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
        quarter = ((int)totalMoney - (halfDollar * 0.5)) / 0.25;
        dime = ((int)totalMoney - (quarter * 0.25)) / 0.1;
        nickel = ((int)totalMoney - (dime * 0.1)) / 0.05;
        penny = ((int)totalMoney - (nickel * 0.05)) / 0.01;

        System.out.println (hundredDollar + " hundred dollar bills");
        System.out.println (fiftyDollar + " fifty dollar bills");
        System.out.println (twentyDollar + " twenty dollar bills");
        System.out.println (tenDollar + " ten dollar bills");
        System.out.println (fiveDollar + " five dollar bills");
        System.out.println (dollar + " one dollar bills or coins");
        System.out.println (halfDollar + " half-dollar coins");
        System.out.println (quarter + " quarter-dollar coins");
        System.out.println (dime + " dimes");
        System.out.println (nickel + " nickel");
        System.out.println (penny + " penny");

    }


}

}

4

4 に答える 4

0

この行で ((int)totalMoney - (tenDollar * 10)) / 5;

キャストは totalMoney だけに適用されます

式全体の値をキャストするには、これを行う必要があります

(int)(totalMoney - (tenDollar * 10)) / 5);
于 2013-09-25T06:35:15.713 に答える
0

問題は次の行にあります。

(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;

とはどういう意味(double)ですか? 変数割り当ての左側 (変数自体) を double にキャストすることはできません。変数は int として宣言されており、変更できません。一度もない。

この行はコンパイルされます (ただし、セマンティクスに関しては正しくない可能性があります)。

halfDollar = (int) ((totalMoney - (dollar * 1)) / 0.5);

それとは別に:私は、プログラムが行うとは思わない、あなたがしたいこと...

于 2013-09-25T06:35:41.770 に答える
0
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;

ハーフドルのダブル キャストで何を達成しようとしていますか。最初にそれを削除します。コードに赤い行がたくさんある理由を見つけるのに 5 分かかりました。

次に、次のような単純なケースを実行できます:-

halfDollar = (int)((totalMoney - (dollar * 1)) / 0.5);

ただし、多くの 10 進数データが​​失われる傾向があるため、double 値を int にキャストすることはお勧めできません。シンプルにすべてpenny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar,twentyDollar, fiftyDollar, hundredDollar;をそのままにしてみませんdoubleか?

于 2013-09-25T06:35:46.393 に答える