-1

我正在尝试做一个简单的计算。我不知道如何在最后一个 if-else 语句中减去“double admissionPrice”。

它指向减法符号给我这个错误消息: operator - cannot be applied to 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);
    }   


    }
}
4

3 回答 3

2

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

于 2013-02-04T02:02:42.293 回答
2

+println()语句中的运算符优先并转换admissionPriceString.

把你的算术运算放在括号里。

于 2013-02-04T02:02:49.407 回答
1
System.out.print("Total price after discount (15%) is : " + (admissionPrice - (admissionPrice * 0.15)));
                                                            ^                                        ^

您需要添加()以修复优先级,否则,如错误所示,您正在减去,而不是从admissionPrice组成的字符串中减去"To....:" + admissionPrice

于 2013-02-04T02:02:28.080 回答