程序如下
import java.io.*;
class Season{
public static void main(String args[])
throws IOException{
int month;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入月份:");
month=br.read();
String season;
if(month==12||month==1||month==2)
season="冬天";
else if(month==3||month==4||month==5)
season="春天";
else if(month==6||month==7||month==8)
season="夏天";
else if(month==9||month==10||month==11)
season="秋天";
else season="不存在的月份";
System.out.println(month+"月份是"+season);
}
}
问题是输入月份:1
之后,显示的是:49月份是不存在的月份
代ç å注éå¦ä¸ï¼
感谢,该程序是正确的,
弱弱地问一句
为什么要用 这个 if (month.equals("12") || month.equals("1") || month.equals("2"))
season = "冬天";
而 用这个 if(month==12||month==1||month==2)
season="冬天";是错的?
字符串间的比较用equals()
本回答被提问者采纳