栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java编程综合练习合集01--买飞机票

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java编程综合练习合集01--买飞机票

需求:

  • 机票价格按照淡季旺季、头等舱和经济舱收费。输入机票原价、月份和头等舱或经济舱。
  • 按照如下规则计算机票价格:旺季(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;
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号