哥哥,你的break呢。厚颜无耻的贴上我刚写的,我也刚学到这里,嘿嘿
- //需求1:利用swicth语句来写一个简易计算器。
- //需求2:利用swicth语句写一个季节判断程序。
- class switchtest
- {
- public static void main(String[] args)
- {
- //简易计算器
- char ch='+';
- int a=3,b=4;
- switch(ch)
- {
- case '+':
- System.out.println(a+"+"+b+"="+(a+b));
- break;
- case '-':
- System.out.println(a+"-"+b+"="+(a-b));
- break;
- case '*':
- System.out.println(a+"*"+b+"="+(a*b));
- break;
- case '/':
- System.out.println(a+"/"+b+"="+(a/b));
- break;
- default:
- System.out.println("Hello World!");
- break;
- }
- //季节判断 3,4,5春季,6,7,8夏季,9,10,11秋季,12,1,2冬季
- int x=7;
- switch(x)
- {
- case 3:
- case 4:
- case 5:
- System.out.println(x+"月属于春季");
- break;
- case 6:
- case 7:
- case 8:
- System.out.println(x+"月属于夏季");
- break;
- case 9:
- case 10:
- case 11:
- System.out.println(x+"月属于秋季");
- break;
- case 12:
- case 1:
- case 2:
- System.out.println(x+"月属于冬季");
- break;
- default:
- System.out.println("Hello World!");
- break;
- }
- }
- }
复制代码 |