java使用for循环求一年的某月某日的天数
生活随笔
收集整理的這篇文章主要介紹了
java使用for循环求一年的某月某日的天数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.atguigu.z_homework.z_homework;import java.util.Scanner;/*
使用for循環求一年的某月某日的天數*/
public class Test10 {public static void main(String[] args) {//定義Scanner對象Scanner sc = new Scanner(System.in);//提示用戶System.out.println("請輸入年份 : ");int year = sc.nextInt();System.out.println("請輸入月份 : ");int month = sc.nextInt();System.out.println("請輸入幾號 : ");int day = sc.nextInt();System.out.println(year + "年" + month + "月" + day + "日");//計算天數for (int i = 1 ; i < month ; i++){//判斷月份天數if (i == 4 || i == 6 || i == 9 || i == 11){day += 30;}else if(i ==1 || i == 3 || i == 5 || i == 7 || i == 8 || i ==10 || i ==12){day += 31;}else if (i == 2){if ((year % 4== 0 && year % 100 !=0) || year % 400 == 0){day += 29;}else{day += 28;}}}System.out.println("是這一年的第 " + day + "天" );}}
總結
以上是生活随笔為你收集整理的java使用for循环求一年的某月某日的天数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学生信息系统管理——优化总结(干货篇二)
- 下一篇: 在有头脑的地方复制淘宝——自由人的自由联