电影票计算

时间:2019-04-30 11:19:24

标签: java

在多厅影院中,有一项折扣计划宣布,当批量预订20张以上的门票时,人们将获得门票总成本10%的折扣,而在电影院的总成本中可获得2%的折扣。如果提交了特殊的优惠券,则可以购买门票。开发一个程序,以根据计划找到总成本。国王级票价为75卢比,女王级票价为150卢比。您也可以选择支付额外的卢比来选择茶点。每个成员50个。

提示:k-king和q-queen,您一次至少要预订5张票,最多要预订40张。如果失败,则显示“最少5张,最多40张票”。如果给圆赋予了'k'或'q'以外的其他值,则输出应为“无效输入”。

机票价格应精确打印到小数点后两位。

  • 样本输入1:
    输入票证编号:35
    要喝茶吗:y
    您有优惠券代码:y
    输入圈子:k
  • 样本输出1:
    票价:4065.25

  • 样本输入2:
    输入票证编号:1

  • 示例输出2:
    最少5张,最多40张门票

这是代码

import java.util.Scanner;
import java.text.DecimalFormat;

public class CinemaTicket {
    public static void main(String[] args) {
        int no, refe, total = 0;
        double cost, sum, sum1, sum2, sum3;
        String ref, co, circle;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the no of ticket:");
        no = s.nextInt();
        if (no < 5 || no > 40) {
            System.out.println("Minimum of 5 and Maximum of 40 tickets");
        }
        System.out.println("Do you want refreshment:");
        ref = s.next();
        System.out.println("Do you have a coupon code:");
        co = s.next();
        System.out.println("Enter the circle:");
        circle = s.next();
        if (circle == "k") {
            total = no * 75;
        } else if (circle == "q") {
            total = no * 150;
        } else {
            System.out.println("Invalid Input");
        }
        if (no > 20) {
            sum = ((0.1) * total);
            sum1 = total - sum;
            if (co == "y") {
                sum2 = ((0.2) * total);
                sum3 = sum1 - sum2;
                if (ref == "y") {
                    refe = no * 150;
                    cost = sum3 + refe;
                } else {
                    cost = sum3;
                }
            } else {
                cost = sum1;
            }
        } else {
            cost = total;
        }
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("Ticket cost:" + df.format(cost));
    }
} 

我尝试了此代码,但是它无法计算出机票的费用。

2 个答案:

答案 0 :(得分:0)

使用String方法equals()或compareTo()。 逻辑运算符不会比较Java中的字符串,因为它不是原始类型。

答案 1 :(得分:0)

您需要做的是:

if (circle.equals("k")) {
            total = no * 75;
        } else if (circle.equals("q")) {
            total = no * 150;
        } else {
            System.out.println("Invalid Input");
        }

不要使用“ ==”,请使用equals方法,它将正常工作。

相关问题