zoukankan      html  css  js  c++  java
  • 编写程序,使用嵌套if选择结构,根据出行的月份和选择的舱位输出实际的机票价格。

     1 import java.util.Scanner;
     2 /**
     3  *
     4  * 功能描述: 编写程序,使用嵌套if选择结构,根据出行的月份和选择的舱位输出实际的机票价格。
     5  *
     6  *
     7  * @Author: apple.
     8  * @Date: 2019/12/3 5:15 PM
     9 */
    10 public class Ticket {
    11     static Scanner sc = new Scanner(System.in);
    12 
    13     public static void main(String[] args) {
    14         int month;//月份
    15         int cabin;//舱位
    16         double price = 5000;
    17         System.out.println("请输入您出行当月份:1~12");
    18         month = sc.nextInt();
    19         System.out.println("请问您选择头等舱还是经济舱?头等舱输入1:经济舱输入2");
    20         cabin = sc.nextInt();
    21         //旺季
    22         if (month >= 4 && month <= 10) {
    23             if (cabin == 1) {
    24                 price = price * 0.9;
    25                 System.out.println("当前享受9折,打完折后:"+price);
    26             } else if (cabin == 2) {
    27                 price = price * 0.6;
    28                 System.out.println("当前享受6折,打完折后:"+price);
    29 
    30             } else {
    31                 System.out.println("error");
    32             }
    33         } else if (month > 0 && month < 13) {//淡季
    34             if (cabin == 1) {
    35                 price = price * 0.5;
    36                 System.out.println("当前享受5折,打完折后:"+price);
    37 
    38             } else if (cabin == 2) {
    39                 price = price * 0.4;
    40                 System.out.println("当前享受4折,打完折后:"+price);
    41 
    42             }
    43         } else {
    44             System.out.println("error");
    45         }
    46         System.out.println("您当机票价格为:" + price);
    47     }
    48 }

    思路:分为两大if便容易让人理解了。

    结果:

  • 相关阅读:
    洛谷P3799 妖梦拼木棒
    bzoj1008 [HNOI2008]越狱
    洛谷P3414 SAC#1
    洛谷P1078 文化之旅
    bzoj1053 [HAOI2007]反素数ant
    洛谷P1588 丢失的牛
    bzoj1085 [SCOI2005]骑士精神
    noip2016 蚯蚓
    noip2016 换教室
    html笔记03:表单
  • 原文地址:https://www.cnblogs.com/appleworld/p/11978169.html
Copyright © 2011-2022 走看看