java swith语句 判断一个月份属于哪个季节

import java.io.IOException;

public class SwitchDemo {

/**
* @param args
*/
public static void main(String[] args) throws IOException
{
int a;
a=(int)System.in.read();
String season;
switch(a)
{
case 12:
case 1:
case 2:
season="冬季";
break;
case 3:
case 4:
case 5:
season="春季";
break;
case 6:
case 7:
case 8:
season="夏季";
break;
case 9:
case 10:
case 11:
season="秋季";
break;
default:
season="错!";
break;
}
System.out.println(a+"月份属于:"+season);
// TODO Auto-generated method stub

}

}

输入8,结果‘56月份属于错!’ 不是应该‘8月份属于夏季’么?
输入d,结果忘了,好像是‘48月份属于错’
该怎么改??
改成了:a=Integer.parseInt(System.in.read());
但是run的时候出错,提示
‘The method parseInt(String) in the type Integer is not applicable for the arguments (int) ’

第1个回答  2011-10-11
从你描述的情况来看,错误不在判断季节上,而在处理输入月份上,比如你输入8为什么会变成56了,这才是问题的症结所在。
第2个回答  2011-10-11
java swith语句 判断一个月份属于哪个季节
第3个回答  2011-10-11
int a;
a=Integer.parseInt(System.in.read());追问

a=Integer.parseInt(System.in.read());
但是run的时候出错,提示
‘The method parseInt(String) in the type Integer is not applicable for the arguments (int) ’

追答

Int a=System.in.read()-48;

原因是输入返回的是字符,不是你要的数字。

本回答被提问者采纳
相似回答