编写JAVA程序,接受用户输入的1~12之间的整数,若不符合条件则重新输入,利用switch语句输出对应月份的天数.

还是JAVA问题,请各位多帮帮忙,感激不尽!

第1个回答  2007-03-16
import java.util.Scanner;

public class Test {
public void testDay() {
Scanner scn = new Scanner(System.in); // 要装JDK5.0以上才能支持
try {
while (true) {
System.out.print("请输入1-12中的一个数:");
int input = scn.nextInt();
switch (input) {
case 1,3,5,7,8,10,12: //大月31天
System.out.println("该月对应天数为:" + 31 + "天");
return;
case 4,6,9,11: //小月30天
System.out.println("该月对应天数为:" + 30 + "天");
return;
case 2: //没输入年所以显示28、29都可以把 @)@
System.out.println("该月对应天数为:" + 29 + "天");
return;
default:
System.out.println("输入数字不在1-12范围内!");
break;
}
}
} catch (RuntimeException e) {
System.out.println("小样,不要欺负我没文化!请重新输入!");
testDay();
}
}
public static void main(String[] args) {
Test test = new Test();
test.testDay();
}
}

参考资料:对1楼答案进行了一点优化

第2个回答  2007-03-15
非闰年

import java.io.*;
public class Test {
/**
* @param args
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String inputStr=null;
String input[]={"1","2","3","4","5","6","7","8","9",
"10","11","12"};
String output[]={"31","28","31","30","31","30",
"31","31","30","31","30","31"};
System.out.print("请输入1-12的整数!");
try {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true){
boolean flag=true;
inputStr=br.readLine();
for(int i=0;i<input.length;i++){
if(inputStr.equals(input[i])){
//有就b标志置false
flag=false;
//跳出循环
break;
}

}
if (!flag)
break;
System.out.print("请重新输入1-12的整数!");
}
for(int j=0;j<input.length;j++){
if(inputStr.equals(input[j]))
System.out.println(input[j]+"月有"+output[j]+"天");
}
} catch (IOException e) {
e.printStackTrace();
}

}

}本回答被网友采纳
相似回答