Java,减去双数据类型

时间:2013-02-04 01:59:54

标签: java double subtraction

我正在尝试进行简单的计算。我无法弄清楚如何在最后的if-else语句中减去“double admissionPrice”。

它指向减法标志,给我这个错误信息: operator - 不能应用于java.lang.String,double

请帮忙。感谢。


import java.text.*;
import java.util.Scanner;


class IMC {
    public static void main(String[] args) {

    int numEmployees = 0;
    double costPerAttendee = 0.00;
    int employeeDiscount;
    double admissionPrice = 0.00;

    Scanner keyboard = new Scanner (System.in);

    System.out.print("Enter amount of employees attending: ");
    numEmployees = keyboard.nextInt();

    System.out.print("Have any employees attended previously? \n For: YES=1 or NO=2"
                        );
    employeeDiscount = keyboard.nextInt();



    if (numEmployees == 1)  { admissionPrice = 695.00;


    } else if (numEmployees == 2 || numEmployees ==  3 ||numEmployees ==  4) { admissionPrice = 545.00;


    } else if (numEmployees >= 5 ||numEmployees >= 6 ||numEmployees >= 7 ||numEmployees >= 8){ admissionPrice = 480.00;


    }  else if (numEmployees >= 9)  { admissionPrice = 395.00;

    }

        System.out.print("The cost per attendee is: " + admissionPrice );


  if (employeeDiscount == 1){
        System.out.print("Total price after discount (15%) is : " + admissionPrice - (admissionPrice * 0.15) );

    } else if (employeeDiscount == 2) {
            System.out.print("No discount. Total price is still: " + admissionPrice);
    }   


    }
}

3 个答案:

答案 0 :(得分:2)

将括号括在(admissionPrice - (admissionPrice * 0.15) )附近。现在,它在尝试减法之前将允许价格连接到"Total price after discount (15%) is : "

答案 1 :(得分:2)

+语句中的println()运算符优先,并将admissionPrice转换为String

将算术运算放在括号中。

答案 2 :(得分:1)

System.out.print("Total price after discount (15%) is : " + (admissionPrice - (admissionPrice * 0.15)));
                                                            ^                                        ^

您需要添加()来修正优先级,否则,正如错误所示,您正在减去,而不是从admissionPrice减去"To....:" + admissionPrice