作者SlashedAxl (Appetite For Illusion)
看板java
标题[问题] 诡异的输出结果
时间Sun Dec 23 20:44:24 2012
小弟是 Java 新手,如果问了蠢问题还请见谅
我写个一个程式,可以由命令列输入年/月来查询该月有几天
虽然说已经成功地将参数值传入了
但是其中的 switch 却好像毫无反应
还要请教各位,到底是发生什麽问题了
=============================程式码==============================
import java.lang.Integer;
public class SwitchDemo2_1 {
public static void main(String[] args) {
int month = Integer.parseInt(args[1]);
int year = Integer.parseInt(args[0]);
int numDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
}
System.out.println("Number of Days = " + numDays);
}}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.254.112.217
1F:推 PsMonkey:隐性违反版规 5。是不管输入什麽都没反应还是怎样? 12/23 20:47
2F:→ SlashedAxl:是正常的执行,输出 Number of Days = 0,也就是预设值 12/23 20:59
3F:→ SlashedAxl:可是 month 和 year 都有值,不知道为什麽没有进switch 12/23 21:01
4F:→ johnhao1206:我用你的程式码跑没这问题耶 12/23 21:15
5F:→ SlashedAxl:好像是我的 JVM 坏掉,因为换一台电脑就好了,谢谢喔! 12/23 21:29
6F:推 PsMonkey:我倒是猜测你 java Foo 的时候值没有给好... 12/23 21:43
7F:→ PsMonkey:加个 default 避免这种可能,也算是 switch 的良好习惯 12/23 21:44
8F:→ SlashedAxl:ok 受教了 12/23 22:32