Java 编写一个程序,询问用户票数和输出总数
编写一个程序,询问用户成人票的数量、儿童票的数量、他们是想要预定票还是普通票,以及他们是否有广播券可以使用。计算订单的成本 门票分为两级,每级55美元,普通门票每级35美元。12岁以下的孩子半价。 当地电台正在播放一个特别节目。如果你打电话来,他们会给你寄一张优惠券,给你20%的折扣 所有超过200美元的订单在最终价格上可获得10%的折扣(在应用其他折扣后),超过400美元的订单可获得15%的折扣 我的代码到目前为止Java 编写一个程序,询问用户票数和输出总数,java,boolean,output,println,Java,Boolean,Output,Println,编写一个程序,询问用户成人票的数量、儿童票的数量、他们是想要预定票还是普通票,以及他们是否有广播券可以使用。计算订单的成本 门票分为两级,每级55美元,普通门票每级35美元。12岁以下的孩子半价。 当地电台正在播放一个特别节目。如果你打电话来,他们会给你寄一张优惠券,给你20%的折扣 所有超过200美元的订单在最终价格上可获得10%的折扣(在应用其他折扣后),超过400美元的订单可获得15%的折扣 我的代码到目前为止 public static void main(String[] args)
public static void main(String[] args) {
// variables
int adultTix;
int childTix;
int GENERAL_ADMISSION = 35;
int RESERVED = 55;
double radioDiscount = .20;
double ticketTotal = 0;
Scanner scan = new Scanner(System.in);
System.out.println("How many adult tickets?");
adultTix = scan.nextInt();
System.out.println("How many kids tickets?");
childTix = scan.nextInt();
scan.nextLine();
System.out.println("Reserved tickets are $55 each and General Admission is $35."
+ " Would you like Reserved or General Admission? (enter GA or RE only):");
String tixType = scan.nextLine();
if (tixType == "GA" || tixType == "ga")
ticketTotal = ((adultTix * GENERAL_ADMISSION) + ((childTix * GENERAL_ADMISSION) / 2));
else if (tixType == "RE" || tixType == "re")
ticketTotal = ((adultTix * RESERVED) + ((childTix * RESERVED) / 2));
System.out.println("Do you have a radio voucher? (enter yes or no):");
String radioQ = scan.nextLine();
if (radioQ == "yes")
System.out.print("With the radio discount, you will save 20%!");
if (radioQ == "no")
System.out.println("No radio discount.");
double radioT;
radioT = ((ticketTotal - (ticketTotal * radioDiscount)));
if (radioT >= 200 && radioT < 400)
System.out.println("With a 10% discount, your total is: $"
+ (radioT * .9));
else if (radioT > 400)
System.out.println("With a 15% discount, your total is: $"
+ (radioT * .85));
scan.close();
}
publicstaticvoidmain(字符串[]args){
//变数
成人智力;
int childTix;
国际综合大学入学率=35;
保留int=55;
双倍折扣=.20;
双票总数=0;
扫描仪扫描=新扫描仪(System.in);
System.out.println(“多少张成人票?”);
adultTix=scan.nextInt();
System.out.println(“多少张儿童票?”);
childTix=scan.nextInt();
scan.nextLine();
预订门票每张55美元,一般入场费35美元
+“您想要预约还是普通入场?(仅输入GA或RE):”;
String tixType=scan.nextLine();
如果(tixType==“GA”| | tixType==“GA”)
票务总额=(成人*普通科入学)+(儿童*普通科入学)/2);
else if(tixType==“RE”| | tixType==“RE”)
票务总额=((成人*保留)+(儿童*保留)/2);
System.out.println(“您有无线电凭证吗?(输入是或否):”;
String radioQ=scan.nextLine();
如果(radioQ==“是”)
System.out.print(“使用收音机折扣,您将节省20%!”;
如果(radioQ==“否”)
System.out.println(“无无线电折扣”);
双无线电;
radioT=((ticketotal-(ticketottal*radioDiscount));
如果(radioT>=200&&radioT<400)
System.out.println(“如果有10%的折扣,您的总额为:$”
+(radioT*.9));
否则,如果(无线电>400)
System.out.println(“如果有15%的折扣,您的总额为:$”
+(radioT*.85));
scan.close();
}
}
正确询问所有问题,但不返回输出。这是一个简单的Java程序,因此我希望得到可能的最简单答案。问题是,如果ifs和elses没有正确触发,因为您不正确地比较了字符串 你需要的不是
if(str==“value”)
而是if(str.equals(“value”))
无处不在。在需要不区分大小写的匹配的情况下,例如if(str==“value”| | str==“value”)
需要if(str.equalsIgnoreCase(“value”)
请查看更多信息,了解两个问题:
- 当测试对象之间是否相等时,应始终使用equals方法,现在使用“==”,这意味着您正在比较两个对象引用。因此,将以下内容从 如果(tixType==“GA”| | tixType==“GA”)
- 询问用户是否有无线凭证后,您应该只进行与无线凭证相关的计算,如:
if (radioQ == "yes")//use equalsIgnoreCase method of String System.out.print("With the radio discount, you will save 20%!"); radioT = ((ticketTotal - (ticketTotal * radioDiscount))); if (radioT >= 200 && radioT < 400) System.out.println("With a 10% discount, your total is: $" + (radioT * .9)); else if (radioT > 400) System.out.println("With a 15% discount, your total is: $" + (radioT * .85)); //apply discount on ticket total? } else ...
if(radioQ==“yes”)//使用字符串的equalsIgnoreCase方法 System.out.print(“使用收音机折扣,您将节省20%!”; radioT=((ticketotal-(ticketottal*radioDiscount)); 如果(radioT>=200&&radioT<400) System.out.println(“如果有10%的折扣,您的总额为:$” +(radioT*.9)); 否则,如果(无线电>400) System.out.println(“如果有15%的折扣,您的总额为:$” +(radioT*.85)); //对门票总额实行折扣吗? }否则。。。
if (radioQ == "yes")//use equalsIgnoreCase method of String
System.out.print("With the radio discount, you will save 20%!");
radioT = ((ticketTotal - (ticketTotal * radioDiscount)));
if (radioT >= 200 && radioT < 400)
System.out.println("With a 10% discount, your total is: $"
+ (radioT * .9));
else if (radioT > 400)
System.out.println("With a 15% discount, your total is: $"
+ (radioT * .85));
//apply discount on ticket total?
} else ...