日期:2014-05-20  浏览次数:20671 次

我是java新手,请教个简单的编程
大家好
我才开始学习java,对一道编程题目不太会,题目是这样的:
使用switch语句编程,确定某一月在哪个季节。
请问下,这到题目该如何来编写呢?
请知道的给我说哈。。谢谢了

------解决方案--------------------
case:1
一季度
case:2
一季度
case:3
一季度
case:4
二季度
...
就这样往下写就可以,很简单的。

------解决方案--------------------
switch(month)
case:1
case:2
case:3 第一季
case:4
case:5
case:6第二季
case:7
case:8
case:9第三季
case:10
case:11
case:12 第四季
------解决方案--------------------
你输 65 66 67 68 都其中1个 就不会input error了
------解决方案--------------------
你用到输入的话建议你用Scanner类!
------解决方案--------------------
lz定义的Month是整型变量,明明是用来保存月数,结果在switch语句中却用 'A ' 'B ' 'C ' 'D '来匹配。
lanseliuying的程序写的是对的。似乎lz对switch语句并不熟悉,不知道该如何使用,建议随便找本Java的书读一读。
------解决方案--------------------
import java.io.*;
public class InputMonth{
public static void main(String args[]) throws IOException{
int Month;

BufferedReader din = new BufferedReader(new InputStreamReader(System.in));

System.out.print( "input Month: ");
Month=Integer.parseInt(din.readLine());
switch(Month){
case 12:
case 1:
case 2:
System.out.println(Month+ " it is winter ");break;
case 3:
case 4:
case 5:
System.out.println(Month+ " it is spring ");
break;
case 6:
case 7:
case 8:
System.out.println(Month+ " it is summer ");
break;
case 9:
case 10:
case 11:
System.out.println(Month+ " it is autume ");
break;
default:
System.out.println( " input error ");
break;
}


}
}


这样写
switch本来就是判断,干嘛还要在if判断呢?
------解决方案--------------------
import java.io.*;
public class InputMonth{
public static void main(String args[]) throws IOException {
int month;

BufferedReader din = new BufferedReader(new InputStreamReader(System.in));

System.out.print( "input Month: ");

month = Integer.parseInt(din.readLine());

if(month > 12 || month < 0) {
System.out.println( "请输入正确的月数 ");
System.exit(-1);
}

switch(month) {
case 3:
case 4:
case 5:
System.out.println(month + " is Spring ");
break;
case 6:
case 7:
case 8:
System.out.println(month + " is Summer ");
break;
case 9:
case 10:
case 11:
System.out.println(month + " is Autume ");
break;
default:
System.out.println(month + " is Winter ");
}

}
}

------解决方案--------------------
你得Month值和case不匹配
import java.io.*;
public class Test{
public static void main(String args[]) throws IOException{
int Month;

BufferedReader din = new BufferedReader(new InputStreamReader(System.in));

System.out.print( "input Month: ");
Month=Integer.parseInt(din.readLine());
if(Month> 12||Month <0)