第1个回答 2008-12-08
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadMonth {
public static Integer howmanyday(int month) {
int monthDay = 0;
switch (month) {
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
monthDay = 31;
break;
case 2:
monthDay = 28;
break;
case 4:case 6:case 9:case 11:
monthDay = 30;
break;
default:
monthDay = -1;
break;
}
return monthDay;
}
public static void main(String[] args) {
int i;
System.out.println("请输入月份(1-12) :");
while (true) {
try {
BufferedReader b = new BufferedReader(new InputStreamReader(
System.in));
String msg = b.readLine();
if ("quit".equals(msg.toString().trim().toLowerCase())) {
System.exit(0);
} else {
i = Integer.parseInt(msg);
if (i > 0 && i < 13) {
System.out.println(i + "月份共有" + howmanyday(i) + "天");
System.out.println("输入月份继续,输入quit退出");
} else {
System.out.println("请输入正确的月份");
}
}
} catch (NumberFormatException e) {
System.out.println("请输入正确的命令,退出请输入 quit");
} catch (IOException e) {
}
}
}
}
第2个回答 2008-12-08
import java.util.*;
public class Test
{
public void howmanyday()
{
Scanner input = new Scanner(System.in);
String quit= "";
do
{
System.out.print("请输入月份:");
int month= input.nextInt();
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(month+"月,有31天!");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(month+"月,有30天!");
break;
case 2:
System.out.println(month+"月,有28天!");
break;
default:
System.out.println("输入错误~~~");
break;
}
System.out.println("退出输入quit,输入其他继续。");
quit = input.next();
}
while (!quit.equals("quit"));
}
public static void main(String args[])
{
Test t = new Test();
t.howmanyday();
}
}
第3个回答 2008-12-08
//:HowManyDays.java
public class HowManyDays {
private static int[] days = {31,28,31,30,31,30,31,31,30,31,30,31,};
public static void main(String[] args) throws Exception{
String exit = "quit";
String usage = "请输入月份:(quit退出)";
String error = "你输入的月份有误.";
while(true){
System.out.println(usage);
byte[] b = new byte[10];
System.in.read(b);
String tmp = new String(b).trim();
try{
if(exit.equals(tmp))System.exit(0);
int month = Integer.parseInt(new String(b).trim());
System.out.println(month+"月份有"+days[month-1]+"天");
}catch(Exception e){
System.out.println(error);
}
}
}
}