我是日文系统,程序显示的时候可能有点乱码。
你这个程序关键是,只要不输入quit,就不能结束程序,所以楼上两位的程序还是不是很完善
不过2楼用的swith case真的不错
我给你做了好长时间,现在不管输入什么都不会出错了一般,包括字符串,数字
程序如下
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test {
// 判断天数
public void howmanyday(int month) {
int day = 0;
// 判断月份的范围,1-12以外的月份
if (month > 12 || month < 1) {
System.out.println("您输入的月份有误,请重新输入");
} else {
if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12) {
day = 31;// 1,3,5,7,8,10,12月的天数是31
System.out.println("您输入的月份是" + month + "共有" + day + "天");
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
day = 30;// 4,6,9,11月是30天
System.out.println("您输入的月份是" + month + "共有" + day + "天");
}
if (month == 2) {
day = 28;// 2月28天
System.out.println("您输入的月份是" + month + "共有" + day + "天");
}
}
}
public static void main(String[] args) throws Exception {
int Month;// 月份
String str;
System.out.println("请您输入月份(1-12) :");
BufferedReader bReader = new BufferedReader(new InputStreamReader(
System.in));
Test test = new Test();
do {
// 屏幕输入月份
str = bReader.readLine();
// 输入quit,结束
if (!str.equals("quit")) {
// 如果输入quit以外的东西,判读是不是数字月份
try {
Month = Integer.parseInt(str);
} catch (Exception e) {
System.out.println("请输入数字月份");
continue;
}
test.howmanyday(Month);
} else {
System.out.println("程序结束");
System.exit(0);
}
} while (1 == 1);
}
}
结果
???入月?(1-12) :
20
您输入的月份有误,请重新输入
0aaaaa
请输入数字月份
5
您输入的月份是5共有31天
2
您输入的月份是2共有28天
quit
程序?束
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) {
}
}
}
}
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();
}
}
//: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);
}
}
}
}
...