龙空技术网

Java从键盘上输入一个月份,然后判断该月份的天数

解影视 169

前言:

此时朋友们对“键盘录入java”大概比较看重,大家都想要了解一些“键盘录入java”的相关文章。那么小编同时在网摘上搜集了一些关于“键盘录入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值。

标签: #键盘录入java