java问题 从键盘输入月份,输出这个月的天数,比如输入是1,则输出31天,要求使用if语句

javajava问题,题目要求用switch语句~

参考代码:
import java.util.Scanner;public class Exercise7 { public static void main(String[] strings) { Scanner input = new Scanner(System.in); int number_Of_DaysInMonth = 0; String MonthOfName = "Unknown"; System.out.print("Input a month number: "); int month = input.nextInt(); System.out.print("Input a year: "); int year = input.nextInt(); switch (month) { case 1: MonthOfName = "January"; number_Of_DaysInMonth = 31; break; case 2: MonthOfName = "February"; if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { number_Of_DaysInMonth = 29; } else { number_Of_DaysInMonth = 28; } break; case 3: MonthOfName = "March"; number_Of_DaysInMonth = 31; break; case 4: MonthOfName = "April"; number_Of_DaysInMonth = 30; break; case 5: MonthOfName = "May"; number_Of_DaysInMonth = 31; break; case 6: MonthOfName = "June"; number_Of_DaysInMonth = 30; break; case 7: MonthOfName = "July"; number_Of_DaysInMonth = 31; break; case 8: MonthOfName = "August"; number_Of_DaysInMonth = 31; break; case 9: MonthOfName = "September"; number_Of_DaysInMonth = 30; break; case 10: MonthOfName = "October"; number_Of_DaysInMonth = 31; break; case 11: MonthOfName = "November"; number_Of_DaysInMonth = 30; break; case 12: MonthOfName = "December"; number_Of_DaysInMonth = 31; } System.out.print(MonthOfName + " " + year + " has " + number_Of_DaysInMonth + " days
"); }}

用 java编写:输入任意年份和月份,输出对应月份的天数,首先判断输入年份是否是闰年,然后使用switch 方法判断月份,判断代码如下:
public class GetDays {
public static int getDays(int year, int month) {int days = 0;boolean isLeapYear = false;if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {System.out.println("这一年是闰年");isLeapYear = true;} else {System.out.println("这一年不是闰年");isLeapYear = false;}switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:days = 31;break;case 2:if (isLeapYear) {days = 29;} else {days = 28;}break;case 4:case 6:case 9:case 11:days = 30;break;default:System.out.println("error!!!");break;}return days;}}

扩展资料在java 语言中switch 语法的使用规则为:
1、switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
2、switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
3、case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
3、当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
参考资料:百度百科—switch

注意一下二月就行了,别的月份都是死的

    Scanner input = new Scanner(System.in);

        int number_Of_DaysInMonth = 0; 
        String MonthOfName = "Unknown";

        System.out.print("Input a month number: ");
        int month = input.nextInt();

        System.out.print("Input a year: ");
        int year = input.nextInt();

if (month == 1){
MonthOfName = "January";
            number_Of_DaysInMonth = 31;
}else if(month == 2){
MonthOfName = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
}else if (month == 3){
MonthOfName = "March";
            number_Of_DaysInMonth = 31;
}


相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网