A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shuibole 中级黑马   /  2015-8-30 21:27  /  210 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.Scanner;
  2. class Test3_SwitchIf {
  3.         public static void main(String[] args) {
  4.                 /*

  5.                 * 键盘录入月份,输出对应的季节
  6.                 一年有四季
  7.                 3,4,5春季
  8.                 6,7,8夏季
  9.                 9,10,11秋季
  10.                 12,1,2冬季
  11.                 */
  12.                 Scanner sc = new Scanner(System.in);        //创建键盘录入对象
  13.                 System.out.println("请输入月份");
  14.                 int month = sc.nextInt();                                //将键盘录入的结果存储在month
  15.                 /*switch (month) {
  16.                 case 3:
  17.                 case 4:
  18.                 case 5:
  19.                         System.out.println(month + "月是春季");
  20.                 break;
  21.                 case 6:
  22.                 case 7:
  23.                 case 8:
  24.                         System.out.println(month + "月是夏季");
  25.                 break;
  26.                 case 9:
  27.                 case 10:
  28.                 case 11:
  29.                         System.out.println(month + "月是秋季");
  30.                 break;
  31.                 case 12:
  32.                 case 1:
  33.                 case 2:
  34.                         System.out.println(month + "月是冬季");
  35.                 break;
  36.                 default:
  37.                         System.out.println("对不起没有对应的季节");
  38.                 break;
  39.                 }*/

  40.                 //用if语句来完成月份对应季节
  41.                 if (month > 12 || month < 1) {
  42.                         System.out.println("对不起没有对应的季节");
  43.                 }else if (month >= 3 && month <= 5) {
  44.                         System.out.println(month + "月是春季");
  45.                 }else if (month >= 6 && month <= 8) {
  46.                         System.out.println(month + "月是夏季");
  47.                 }else if (month >= 9 && month <= 11) {
  48.                         System.out.println(month + "月是秋季");
  49.                 }else {
  50.                         System.out.println(month + "月是冬季");
  51.                 }
  52.         }
  53. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马