java编程。订购机票,机票的价格受到季节旺季。淡季影响,而且头等藏跟经济舱的价格也不一同。
答案:3 悬赏:70 手机版
解决时间 2021-03-16 18:43
- 提问者网友:疯子也有疯子的情调
- 2021-03-16 04:39
假设机票原价为5000元,4-10月为旺季.旺季头等舱打9折 经济舱打八折 淡季头等舱打5这 经济舱打4折 编写程序 用IF选择结构 根据出行的月份和选择的舱位输出实际的机票价格。
最佳答案
- 五星知识达人网友:洎扰庸人
- 2021-03-16 05:40
public class Ticket {
private int month;
private int classLevel; //1: 头等舱, 2: 经济舱
private int price;
private int basePrice = 5000; //原价
public Ticket(int month, int classLevel) {
this.month = month;
this.classLevel = classLevel;
}
public void showMeThePrice() {
//旺季月份: 4-10
if ((month >= 4) && (month <= 10)) {
if (classLevel == 1) {
price = basePrice * 0.9;
System.out.println("Month: " + month + "; Class: " + classLevel + "; Price: " + price);
} else if (classLevel == 2) {
price = basePrice * 0.8;
System.out.println("Month: " + month + "; Class: " + classLevel + "; Price: " + price);
}
}
// 淡季月份: 1,2,3,11,12
if ((month >= 1) && (month <= 3) || month = 11 || month = 12) {
if (classLevel == 1) {
price = basePrice * 0.5;
System.out.println("Month: " + month + "; Class: " + classLevel + "; Price: " + price);
} else if (classLevel == 2) {
price = basePrice * 0.4;
System.out.println("Month: " + month + "; Class: " + classLevel + "; Price: " + price);
}
}
}
}
测试类:
public class Test {
Ticket myTicket = new Ticket(4, 1); //例如:四月,头等舱
myTicket.showMeThePrice();//输出显示价格
...
}
以上代码全部手打,因为公司没有安装jdk,所以无法测试,你自己调试吧。
private int month;
private int classLevel; //1: 头等舱, 2: 经济舱
private int price;
private int basePrice = 5000; //原价
public Ticket(int month, int classLevel) {
this.month = month;
this.classLevel = classLevel;
}
public void showMeThePrice() {
//旺季月份: 4-10
if ((month >= 4) && (month <= 10)) {
if (classLevel == 1) {
price = basePrice * 0.9;
System.out.println("Month: " + month + "; Class: " + classLevel + "; Price: " + price);
} else if (classLevel == 2) {
price = basePrice * 0.8;
System.out.println("Month: " + month + "; Class: " + classLevel + "; Price: " + price);
}
}
// 淡季月份: 1,2,3,11,12
if ((month >= 1) && (month <= 3) || month = 11 || month = 12) {
if (classLevel == 1) {
price = basePrice * 0.5;
System.out.println("Month: " + month + "; Class: " + classLevel + "; Price: " + price);
} else if (classLevel == 2) {
price = basePrice * 0.4;
System.out.println("Month: " + month + "; Class: " + classLevel + "; Price: " + price);
}
}
}
}
测试类:
public class Test {
Ticket myTicket = new Ticket(4, 1); //例如:四月,头等舱
myTicket.showMeThePrice();//输出显示价格
...
}
以上代码全部手打,因为公司没有安装jdk,所以无法测试,你自己调试吧。
全部回答
- 1楼网友:拜訪者
- 2021-03-16 07:11
int i =4;
if i =<10 {
语句
}
else
{
语句
}
- 2楼网友:人類模型
- 2021-03-16 06:58
public double getfirstclassprice(double baseprice) {
return ismidseason() ? baseprice * 0.9 : baseprice * 0.5;
}
public double gettouristclassprice(double baseprice) {
return ismidseason() ? baseprice * 0.8 : baseprice * 0.4;
}
private boolean ismidseason() {
int month = calendar.getinstance().get(calendar.month) + 1;
return month >= 4 && month <= 10;
}
上面那个好看些。看了下你的要求,貌似不太符合- -。
重写了个
public double getprice(double baseprice, int month, boolean isfirstclass) {
double result;
if (month >= 4 && month <= 10) {
if (isfirstclass) {
result = baseprice * 0.9;
} else {
result = baseprice * 0.8;
}
} else {
if (isfirstclass) {
result = baseprice * 0.5;
} else {
result = baseprice * 0.4;
}
}
return result;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯