黑马程序员技术交流社区

标题: 输出该月份所对应的季节的if语句与swicth 语句的实现 [打印本页]

作者: STARlove    时间: 2015-8-7 23:46
标题: 输出该月份所对应的季节的if语句与swicth 语句的实现
本帖最后由 STARlove 于 2015-8-7 23:50 编辑

先   明确下  简单的  Scanner 使用:步骤为:        A,导包
                import java.util.Scanner;
        B,创建对象,封装键盘录入
                Scanner sc = new Scanner(System.in);
        C,调用方法,获取数据
                int number = sc.nextInt();


作者: STARlove    时间: 2015-8-7 23:47
import java.util.Scanner;

class MonthTest {
        public static void main(String[] args) {
                //String y;
                //封装键盘录入
                Scanner sc = new Scanner(System.in);
                //提示语句
                System.out.println("请输入一个月份(1--12):");
                //调用方法
                int month = sc.nextInt();
                String y;
               
                if(month >= 3 && month <= 5){
                        y = "春季";
                }else if(month >= 6 && month <= 8){
                        y = "夏季";
                }else if(month >= 9 && month <= 11){
                        y = "秋季";
                }else if(month == 1 || month == 2 || month ==12){
                        y = "冬季";
                }else{
                        y="error";
                }
                System.out.println(month+"月份是:"+y);
               
        }
}

作者: STARlove    时间: 2015-8-7 23:48
/*
                要求:根据键盘录入的月份,输出该月份所对应的季节。

                思路:
                A\键盘输入,就需要导包
                        import java.util.Scanner;
                B\建立一个类,名称为MonthDemo2
                C\使用switch就可以实现。
                        switch(表达式){
                                case 值1:
                                        语句
                                        break;
                                case 值2:
                                        语句
                                        break;
                                case 值3:
                                        语句
                                        break;
                                ... ...
                                default:
                                        语句
                                        break;
                        }

*/

import java.util.Scanner;//导包

class  MonthMonth
{
        public static void main(String[] args)
        {
                //创建对象,封装键盘输入
                Scanner sc = new Scanner(System.in);
                //提示语句
                System.out.print("输入一个月份(1-->12):");
                //调用方法,获取数据
                int month = sc.nextInt();

                //switch 语句
                switch(month){
                        case 3:
                        case 4:
                        case 5:
                                System.out.println("月份"+month+"是夏季");break;
                        case 6:
                        case 7:
                        case 8:
                                System.out.println("月份"+month+"是夏季");break;
                        case 9:
                        case 10:
                        case 11:
                                System.out.println("月份"+month+"是秋季");break;
                        case 1:
                        case 2:
                        case 12:
                                System.out.println("月份"+month+"是冬季");break;
                        default:
                                System.out.println("月份"+month+"就不是月份该有的数值!@!#!$$!#$!#$#!%$!#");break;
                }
                System.out.println("继续要重新运行呦!!'.'");
        }
}

作者: wang949055945    时间: 2015-8-8 01:07
挺详细啊,
我也刚学了这个,也知道了switch语句中的case穿透
作者: STARlove    时间: 2015-8-8 09:09
wang949055945 发表于 2015-8-8 01:07
挺详细啊,
我也刚学了这个,也知道了switch语句中的case穿透

一起加油哈哈哈哈   
作者: F灬灬    时间: 2015-8-8 16:39
import java.util.Scanner;
class SwitchDemo{
        public static void main(String[] args){
                for(int i=0;i<10;i++){
                System.out.println("input numbers of the month !");
                Scanner input = new Scanner(System.in);
                int month = input.nextInt();
                                switch(month){
                                case 3:
                                case 4:
                                case 5:
                                System.out.println("Spring");
                                break;
                                case 6:
                                case 7:
                                case 8:
                                System.out.println("Summer");
                                        break;
                                case 9:
                                case 10:
                                case 11:
                                System.out.println("Autumn");
                                        break;
                                case 12:
                                case 1 :
                                case 2 :
                                System.out.println("Winter");
                                break;
                                default:System.out.println("input error !");
                                break;
                        }
                       
                }
        }
}
一模一样
作者: HwH199209    时间: 2015-8-8 20:41
F灬灬 发表于 2015-8-8 16:39
import java.util.Scanner;
class SwitchDemo{
        public static void main(String[] args){

写的很好啊!!!执行循环,能够多次查询输入的月份!赞一下{:2_35:}
作者: STARlove    时间: 2015-8-8 21:21
HwH199209 发表于 2015-8-8 20:41
写的很好啊!!!执行循环,能够多次查询输入的月份!赞一下

哈哈{:3_57:}
作者: 3174918418    时间: 2015-8-8 21:23
顶一下, 写得不错
作者: STARlove    时间: 2015-8-8 21:25
3174918418 发表于 2015-8-8 21:23
顶一下, 写得不错

System.out.print("阿里嘎多!");
作者: ℃葫芦    时间: 2015-8-8 21:36
还没学到包,先马克一个  大致能看个差不多。
作者: STARlove    时间: 2015-8-8 21:54
℃葫芦 发表于 2015-8-8 21:36
还没学到包,先马克一个  大致能看个差不多。

我也刚刚基础班学了几天。。这些简单包,先记下  就好。。觉得挺好使的




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2