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;
    		}	
    	}
    }
    
  • 相关阅读:
    linux文件IO操作篇 (二) 缓冲文件
    信号量和互斥锁的区别
    linux 无锁化编程
    C语言中 time相关的函数 头文件
    printf 打印字体和背景带颜色的输出的方法
    在win10环境下安装Cygwin,可以GCC编译
    学习《大话存储》
    linux内核态和用户态的信号量
    学习Makefile
    git 环境搭建
  • 原文地址:https://www.cnblogs.com/Archer314/p/14515830.html
Copyright © 2011-2022 走看看