• 项目实战--实现淡旺季飞机票打折


    1.项目案例

    某航空公司为吸引更多的顾客推出了优惠活动。原来的飞机票价为 60000 元,活动时,4~11 月旺季,头等舱 9 折,经济舱 8 折;1~3 月、12 月淡季,头等舱 5 折,经济舱 4 折,求机票的价格。

    2.项目实现

    2.1用if-else语句实现淡旺季飞机票打折

    import java.util.Scanner;
    public class plan01{
     	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		System.out.print("The month in which you want to buy tickets:");
    		int month =sc.nextInt();
    		System.out.print("What cabin do you want to buy(1. First class 2. Economy class):");
    		int choice=sc.nextInt();
    		double resault=60000;
            	if((month>=4&&month<=11)||(month==12)){
    			if(choice==1){
                      		resault=0.9*resault;
    			}else{
    	  		resault=0.8*resault;}
             		}
           		if(month>=1&&month<=3){
    			if(choice==1){
                      		resault=0.5*resault;
    			}else{
    	 		 resault=0.4*resault;}
            		}
           		 if(choice==1){
    		System.out.println("The first class fare is "+resault);}
    		else{System.out.println("The economy class fare is "+resault);}
    	}
    }
    

    2.2用switch-case语句实现淡旺季飞机票打折

    上面是if-else语句嵌套实现的淡旺季飞机票打折,下面我们用switch语句实现,代码如下

    import java.util.Scanner;
    public class plan02{
    	public static void main(String[] args){
    		Scanner s = new Scanner(System.in);
    		System.out.print("The month in which you want to buy tickets: ");
    		int month = s.nextInt();
    		System.out.print("What cabin do you want to buy(1. First class 2. Economy class): ");
    		int choice = s.nextInt();
    		double resault=60000;
    		switch(month){
    		case 1:
    		case 2:
    		case 12:
    		case 3:
    			switch(choice){
    				case 1:
    					resault=0.5*resault;
    					break;
    				case 2:
    					resault=0.4*resault;	
    					break;				
    			}
    			break;
    		case 4:
    		case 5:
    		case 6:
    		case 7:
    		case 8:
    		case 9:
    		case 10:
    		case 11:
    			switch(choice){
    				case 1:
    					resault=0.9*resault;
    					break;
    				case 2:
    					resault=0.8*resault;	
    					break;				
    			}
    			break;
    		}
    		
    		switch(choice){
    		case 1:
    			System.out.println("The first class fare is "+resault);
    			break;
    		case 2:
    			System.out.println("The economy class fare is "+resault);
    			break;
    		}	
    	}
    }
    
  • 相关阅读:
    7.24总结
    7.23总结
    7.22总结
    。。。
    7.21总结
    7.20总结
    7.19总结
    大假期第四次测试总结
    大假期第三次测试
    题目分享k
  • 原文地址:https://www.cnblogs.com/Archer314/p/14515830.html
Copyright © 2020-2023  润新知