zoukankan      html  css  js  c++  java
  • 项目实战--实现淡旺季飞机票打折

    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;
    		}	
    	}
    }
    
  • 相关阅读:
    iOS-按钮的代码封装
    MAC_talk 笔记-之mac使用技巧
    关于简历
    win7系统下VS2010配置glew
    NOIP模拟:饼干(简单规律推导)
    c++ string and wstring conversion
    c++ 使用PID获取可执行文件路径
    c++ 使用PID获取顶级窗口句柄和标题
    c++ 去掉字符串首尾空格
    git include只包含某些文件
  • 原文地址:https://www.cnblogs.com/Archer314/p/14515830.html
Copyright © 2011-2022 走看看