需求:
- 机票价格按照淡季旺季、头等舱和经济舱收费。输入机票原价、月份和头等舱或经济舱。
- 按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。
代码如下:
package test1;
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.print("请输入机票原价格:");
int price=sc.nextInt();
System.out.print("请输入月份:");
int month=sc.nextInt();
System.out.print("请输入舱位(0-经济舱;1-头等舱):");
int seat=sc.nextInt();
double nowPrice=0.0;
nowPrice=getPrice(price,month,seat);
System.out.println(nowPrice);
}
//编写方法计算机票价格
public static double getPrice(int price,int month,int seat) {
double nowPrice=0.0;
if(month>=5 && month<=10) {
if(seat==1) {
nowPrice=price*0.9;
}else if(seat==0){
nowPrice=price*0.85;
}else {
System.out.println("输入舱位错误!");
}
}else if(month==11 || month==12 || (month>=1 && month<=4)) {
if(seat==1) {
nowPrice=price*0.7;
}else if(seat==0){
nowPrice=price*0.65;
}else {
System.out.println("输入舱位错误!");
}
}
else {
System.out.println("输入月份错误!");
}
return nowPrice;
}
}



