0

我试图对涉及变量的公式进行简单的数学计算。但是,编译器中会弹出一个错误,表明变量类型不匹配。我尝试过强制转换和更改变量类型,但它们不起作用。如何在不破坏代码基本格式的情况下解决此问题。

我在 Java 方面没有经验,所以任何指针都会有所帮助。

这是代码。这是一个货币转换程序。错误出现在代码的第二部分,即 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

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);

您还需要改进代码的逻辑,我可以清楚地看到它不符合您想要实现的目标。

于 2013-09-25T06:41:57.500 回答
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)?您不能将变量赋值(变量本身)的左侧转换为双精度。该变量被声明为 int,并且不能更改。绝不。

此行编译(但在语义方面可能不正确):

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

除此之外:我不认为,程序确实,你想要它做什么......

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

你想用 halfDollar 的双重演员来实现什么。先把那个去掉。我花了 5 分钟才弄清楚为什么你的代码中有这么多红线。

接下来,您可以做一个这样的简单案例:-

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

但是不建议将 double 值转换为 int,因为您可能会丢失很多十进制数据。为什么不简单地拥有penny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar,twentyDollar, fiftyDollar, hundredDollar;一切double

于 2013-09-25T06:35:46.393 回答