标签:util lazy div mon can ack day pac 执行
package com.cx.Switch; import java.util.Scanner; /** * 计算出某年某月某日是今年的第几天 * 使用switch */ public class SwitchDemo { public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int sumDays=0; System.out.println("年:"); int year=scanner.nextInt(); System.out.println("月:"); int month=scanner.nextInt(); System.out.println("日:"); int day=scanner.nextInt(); switch(month){ case 12: sumDays+=30; case 11: sumDays+=31; case 10: sumDays+=30; case 9: sumDays+=31; case 8: sumDays+=31; case 7: sumDays+=30; case 6: sumDays+=31; case 5: sumDays+=30; case 4: sumDays+=31; case 3: if((year%4==0&&year%10!=0)||(year%400==0)){ sumDays+=29; }else { sumDays+=28; } case 2: sumDays+=31; case 1: sumDays=day; } System.out.println(year+"年"+month+"月"+day+"日"+"是当年的第"); System.out.println(sumDays+"天"); } }
输出结果一直是day,调试发现执行51行时候sumDays是181
但是继续下一步发现
发现把case1 :sumDays=day改成即可,
case 1: sumDays+=day;
sumDays+=sumDays------>sumDays=sumDays+day;sumDays是上一步的sumDays结果,不是0;
使用switch计算出某年某月某日是今年的第几天,输出一直是当月天数
标签:util lazy div mon can ack day pac 执行
原文地址:https://www.cnblogs.com/pengsulong/p/13337986.html