Java从键盘上输入一个月份,然后判断该月份的天数
输入
6
输出
6月份为30天
这是对的
package com;
import java.io.*;
public class app4_4 {
public static void main(String[] args)throws Exception
{
int days;int month;
BufferedReader buf;
InputStreamReader inp;
inp=new InputStreamReader(System.in);
buf=new BufferedReader(inp);
System.out.println("请输入月份:");
month=Integer.parseInt(buf.readLine());
switch((int)month) {
case 2:
days = 28;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
default:
days = 30;
}
System.out.println(month+"月份为"+days+"天");
}
}
错误示范
package com;
public class app4_4 {
public static void main(String[] args)throws Exception
{
int days;char month;
System.out.println("请输入月份:");
month=(char)System.in.read();
switch(month) {
case 2:
days = 28;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
default:
days = 30;
}
System.out.println(month+"月份为"+days+"天");
}
}
错误示范
package com;
public class app4_4 {
public static void main(String[] args)throws Exception
{
int days;int month;
System.out.println("请输入月份:");
month=(char)System.in.read();
switch(month) {
case 2:
days = 28;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
default:
days = 30;
}
System.out.println((char)month+"月份为"+days+"天");
}
}
System.out.println((char)month+"月份为"+days+"天");
month前面需要加char,不然会打印出ascll值。