java学习(26):switch
/擴展練習:
任務5:switch語句
編寫控制臺Java程序,使用Scanner對象相關方法接收用戶輸入的年份和月份,
輸出這個月有多少天。(提示:閏年計算方法為:能被4整除單不能被100整除;或者能被400整除就是閏年。)/
import java.util.Scanner;
public class test05{
public static void main(String[] args){
System.out.println(“請輸入一個年份”);
Scanner in = new Scanner(System.in);
int year = in.nextInt();
System.out.println(“請輸入一個月份”);
int month=in.nextInt();
if((year%40&&year%100!=0)||(year%4000))
{
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(“這個月有31天”);
break;
case 2:
System.out.println(“這個月有29天”);
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(“這個月有30天”);
break;
default:
System.out.println(“不存在”);
break;
}
}else{
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(“這個月有31天”);
break;
case 2:
System.out.println(“這個月有28天”);
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(“這個月有30天”);
break;
default:
System.out.println(“不存在”);
break;
}
}
}
總結
以上是生活随笔為你收集整理的java学习(26):switch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [在线小说系统源码]精品微信小程序小说阅
- 下一篇: 在线小说阅读器