2020-09-18

计算某年某月的地铁费

解决现实问题:

小王在IT公司工作,每天坐地铁上班,从家到公司的地铁单程无折扣票价为6元。地铁的优惠政策是每月消费满100后8折,满150后5折,过月恢复无折扣。假设小王是双休,周末无加班,不考虑其他法定节假日。开发程序计算出任意某个月份中小王在地铁上下班花费的总费用

 package com.sbs;

public class mryl917 {

	public static void main(String[] args) {
		Times t1 = new Times(2020,9);
		int x = t1.times();
		double sum = 0;
		double price = 6;
		for(int i = 0;i < x;i++) {
			sum += price;
			if(sum>150) {
				price = 6*0.5;
			}
			else if(sum>100) {
				price = 6*0.8;
			}				
		}	
		System.out.println( t1.year+"年"+t1.month+"月地铁费为:"+sum);
		
			
	}

}
class Times {  
	   int year;
	   int month;
	Times ()	{}
 Times (int year,int month){
		this.year = year;
		this.month = month;	
 }
	int times() {		 
		switch(month){ 
			case 1:  
		    case 3: 
			case 5: 
			case 7: 
			case 8: 
			case 10:  
			case 12: 	    
				return  54;	      		 
			case 4: 
		    case 6: 
			case 9: 
			case 11:  	    	 
		      	return 	   52; 
			case 2: 
				//年份能被4整除而不能被100整除,或能被400整出为闰年,2月有29天
			    if((year%4 == 0 && year%100 != 0) || year%400 == 0)	{		
			    	return   50;
				}
				else {
					return   48; 
				}				   
		}	
		return  -1;
	}
}	
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页