首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电影票计算

电影票计算
EN

Stack Overflow用户
提问于 2019-04-30 11:19:23
回答 2查看 15.4K关注 0票数 1

在一间多层影院,宣布了一项折扣计划,当大量订票超过20张时,可从总票价中获得10%的折扣;如果提交一张特别优惠券卡,则在门票总费用上有2%的折扣。根据计划开发一个程序来找出总成本。王位机票的价格是75卢比,皇后级的票价是150卢比。茶点也可以通过支付额外的卢比来选择。每名成员50人。

提示: k-king和q-queen,您必须至少预定5张票,每次最多订40张票。如果出现故障,则显示“最少5张,最多40张”。如果给圆的值不是'k‘或'q’,输出应该是“无效输入”。

机票费用应准确打印到小数点后两位。

  • 样本输入1: 输入车票编号:35 你想要茶点吗? 你有优惠券号码吗? 输入圆圈:K
  • 样本输出1: 票价:4065.25
  • 样本输入2: 输入车票编号:1
  • 样本输出2: 最少5张,最多40张

这是密码

代码语言:javascript
复制
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));
    }
} 

我试过这段代码,但它不计算票的成本。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-30 11:35:44

使用String方法等于()或compareTo()。逻辑运算符不会比较java中的字符串,因为它不是一个基本类型。

票数 0
EN

Stack Overflow用户

发布于 2019-04-30 11:46:18

你要做的就是:

代码语言:javascript
复制
if (circle.equals("k")) {
            total = no * 75;
        } else if (circle.equals("q")) {
            total = no * 150;
        } else {
            System.out.println("Invalid Input");
        }

不要使用"==",使用相等的方法,它会工作得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55919783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档