编写JAVA程序,接受用户输入的1~12之间的整数,若不符合条件则重新输入,利用switch语句输出对应月份的天数. 5
2个回答
展开全部
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();
}
}
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楼答案进行了一点优化
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
非闰年
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();
}
}
}
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();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询