• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wchenfeng

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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值。

posted on 2022-04-12 20:02  王陈锋  阅读(195)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3