当前位置: 首页 > 面试题库 >

用Java计算leap年

马星阑
2023-03-14
问题内容

这是家庭作业,我已经获得了成绩,但是我没有在代码中实现leap年。这是一个简单的程序,可以根据用户输入显示一个月内的数字。我唯一不知道的是一种实现implement年的方法,这种writing年将在2月获得29天而不是28天,而无需编写多条if语句。当然有更简单的方法吗?这是代码:

//Displays number of days in a month

package chapter_3;

import java.util.Scanner;


public class Chapter_3 {

    public static void main(String[] args) {
        System.out.println("This program will calculate \n"
                + "the number of days in a month based upon \n"
                + "your input, when prompted please enter \n"
                + "the numerical value of the month you would like\n"
                + "to view including the year. Example: if you would like\n"
                + "to view March enter 3 2011 and so on.");

        Scanner input = new Scanner(System.in);

        //Prompt user for month and year
        System.out.print("Please enter the month and year: ");
        int month = input.nextInt();
        int year = input.nextInt();

        if (month == 1) {
            System.out.println("January " + year + " has 31 days");
        }   
        else if (month == 2) {
            System.out.println("February " + year + " has 28 days");
        }   
        else if (month == 3) {
            System.out.println("March " + year + " has 31 days");
        }
        else if (month == 4) {
            System.out.println("April " + year + " has 30 days");
        }
        else if (month == 5) {
            System.out.println("May " + year + " has 31 days");
        }
        else if (month == 6) {
            System.out.println("June " + year + " has 30 days");
        }
        else if (month == 7) {
            System.out.println("July " + year + " has 31 days");
        }
        else if (month == 8) {
            System.out.println("August " + year + " has 31 days");
        }
        else if (month == 9) {
            System.out.println("September " + year + " has 30 days");
         }
        else if (month == 10) {
            System.out.println("October " + year + " has 30 days");
        }
        else if (month == 11) {
            System.out.println("November " + year + " has 30 days");
        }
        else if (month == 12) {
            System.out.println("December " + year + " has 31 days");
         }
        else {
            System.out.println("Please enter the numerical value "
                + "of the month you would like to view");
        }
    }
}

问题答案:

如果使用GregorianCalendar,则可以执行以下操作

确定给定年份是否为is年。如果给定的年份是a年,则返回true。要指定BC年号,必须提供1年号。例如,BC 4年被指定为-3。

GregorianCalendar cal = new GregorianCalendar();

if(cal.isLeapYear(year))
{
    System.out.print("Given year is leap year.");
}
else
{ 
    System.out.print("Given year is not leap year.");
}


 类似资料:
  • 问题内容: 这是我所拥有的: 我是编程新手,所以如果这段代码看起来不成熟,我不会感到惊讶。无论如何,我让用户输入了一年零一个月(前三个字母)。我为a年创建了一个布尔变量,该变量表示用户输入的任何年份都需要被4、100和400整除。然后,我创建了一个if语句,以确认是否是a年才能打印出“ Feb(任何年份用户输入)中有DaysLeapYear。” 我认为我的算法有问题,因为如果我要取出TwentyE

  • 问题内容: 我正在解析一些具有the秒时间戳记datetime的数据。我使用以下代码来解析该字符串并将其转换为datetime对象: Python文档声称这不应该成为接受的问题。但是,我在上面的时间戳中收到此错误 谢谢 问题答案: 做这个: 输出为:

  • The Leap day theme Leap day is a Jekyll theme for GitHub Pages. You can preview the theme to see what it looks like, or even use it today. Usage To use the Leap day theme: Add the following to your si

  • Orange Leap (前身是 MPowerOpen) 是第一个全功能的开源的组织关系管理系统,适用于任意规模的非盈利组织使用。

  • 我正在制作一个方法,该方法应该计算一定时期内某个金额的利率(这些值已经在参数中定义)。这是我到目前为止拥有的代码: 我正在制作嵌套的for循环,正如您在那里看到的那样,那里缺少代码。这里我遇到了一些麻烦。第一个for循环贯穿年份,另一个应该计算总量。为了清楚变量“int年份”中定义的年份,假设它是7,那么程序应该计算每年数量的增长,因此: 主要方法如下所示: 我很感激我能得到的任何帮助!

  • 问题内容: 该方法在Java中返回什么值? 我读到它是一个对象的内存引用…的哈希值是1;的哈希值是97。 我很困惑:是ASCII还是什么类型的值? 问题答案: 哈希码是一个整数值,表示被调用的对象的状态。这就是为什么将设置为1的an 返回哈希码“ 1”的原因,因为哈希码及其值是相同的。字符的哈希码等于其ASCII字符码。如果编写自定义类型,则负责创建一个最佳实现,该实现将最能代表当前实例的状态。