这里写自定义目录标题
欢迎使用Mark`package example;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
//机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。
// 按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折
public class ex1
{
public static void main(String[] args)
{
System.out.println(“请问您买的是头等仓还是经济仓:”);
Scanner str=new Scanner(System.in);
String warehouse= str.next();
System.out.println("请输入您的机票原价:");
Scanner sc=new Scanner(System.in);
double cost= sc.nextInt();
System.out.println("请说出现在的月份:");
Scanner st=new Scanner(System.in);
int month= st.nextInt();
if(Objects.equals(warehouse, "头等仓"))
{
if(month>=5 && month<=10)
{
cost = cost * 0.9;
System.out.println("您的票价是:" + cost);
}
else
{
cost =cost*0.7;
System.out.println("您的票价是:"+cost);
}
}
else if(Objects.equals(warehouse, "经济仓"))
{
if(month>=5 && month<=10)
{
cost = cost * 0.85;
System.out.println("您的票价是:" + cost);
}
else
{
cost =cost*0.65;
System.out.println("您的票价是:"+cost);
}
}
}
}
使用if语句,代码杂乱,嵌套太多,占用内存,新手上路还望大佬指点。
下面是人工智能写出来的代码:
import java.util.Scanner;
public class AirlineTicketPriceCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入机票原价:");
double originalPrice = input.nextDouble();
System.out.print("请输入月份(1-12):");
int month = input.nextInt();
System.out.print("请输入舱位等级(头等舱或经济舱):");
String cabinClass = input.next();
double discount = 1.0;
if (month >= 5 && month <= 10) { // 旺季
if (cabinClass.equals("头等舱")) {
discount = 0.9;
} else if (cabinClass.equals("经济舱")) {
discount = 0.85;
}
} else { // 淡季
if (cabinClass.equals("头等舱")) {
discount = 0.7;
} else if (cabinClass.equals("经济舱")) {
discount = 0.65;
}
}
double finalPrice = originalPrice * discount;
System.out.printf("最终机票价格为:%.2f", finalPrice);
}
}