在一间多层影院,宣布了一项折扣计划,当大量订票超过20张时,可从总票价中获得10%的折扣;如果提交一张特别优惠券卡,则在门票总费用上有2%的折扣。根据计划开发一个程序来找出总成本。王位机票的价格是75卢比,皇后级的票价是150卢比。茶点也可以通过支付额外的卢比来选择。每名成员50人。
提示: k-king和q-queen,您必须至少预定5张票,每次最多订40张票。如果出现故障,则显示“最少5张,最多40张”。如果给圆的值不是'k‘或'q’,输出应该是“无效输入”。
机票费用应准确打印到小数点后两位。
这是密码
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));
}
} 我试过这段代码,但它不计算票的成本。
发布于 2019-04-30 11:35:44
使用String方法等于()或compareTo()。逻辑运算符不会比较java中的字符串,因为它不是一个基本类型。
发布于 2019-04-30 11:46:18
你要做的就是:
if (circle.equals("k")) {
total = no * 75;
} else if (circle.equals("q")) {
total = no * 150;
} else {
System.out.println("Invalid Input");
}不要使用"==",使用相等的方法,它会工作得很好。
https://stackoverflow.com/questions/55919783
复制相似问题