为什么我的程序只输出一个结果而不是五个? [英] Why does my program only outputs one result instead five?

查看:121
本文介绍了为什么我的程序只输出一个结果而不是五个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/*
 *Find if a year is leap or not
 */

public class LeapYear{

    private static int leapYear;

    public void setLeapYear(int leapYear){

        this.leapYear = leapYear;

    }// end method


    public static void main (String[] args) {
        LeapYear leap = new LeapYear();
        leap.setLeapYear(2010);
        leap.setLeapYear(2008);
        leap.setLeapYear(1900);
        leap.setLeapYear(2000);
        leap.setLeapYear(1565);

        // Is it Divisible by 4?
        if (leapYear % 4 == 0) {

            // Is it Divisible by 4 but not 100?
            if (leapYear % 100 != 0) {
                System.out.println(leapYear + ": is a leap year.");
            }
            // Is it Divisible by 4 and 100 and 400?
            else if (leapYear % 400 == 0) {
                System.out.println(leapYear + ": is a leap year.");
            }
            // It is Divisible by 4 and 100 but not 400!
            else {
                System.out.println(leapYear + ": is not a leap year.");
            }
        }
        // It is not divisible by 4.
        else {
            System.out.println(leapYear + ": is not a leap year.");
        }
    }
}

我是Java新手我编写了这段代码,这样它就可以调用所有五年的布尔值并为所有这些代码生成答案。但是它只调用最后一个。我该怎么做?

I am new to Java and I wrote this code so that it would call for all five years into the boolean and generate answers for all of them. However it only calls the last one. How would I do this right?

推荐答案

您需要每年使用单独的对象或至少将闰年检查方法称为当你为那一年的对象创建时。

You need use separate object for each year or at least call the Leap Year checking method as soon as you crated the object for that year.

你所拥有的是对函数的一系列调用,该函数为同一对象的属性赋值。因此,只有最后一个语句才有效,因为先前的值会被覆盖。

What you have is a series of call to a function that assigns a value to an attribute of the same object. Therefore, only the last statement has the effect as previous values are overwritten.

另外请注意,您的代码似乎没有正确组织。乳清你在Main进行检查,似乎 leapYear 没有在任何地方定义。

On additional note, your code doesn't seem to be properly organized. Whey do you make the checkings in Main and it seems that leapYear isn't defined anywhere.

也许,你可能想要定义一个函数,该函数返回true / false,具体取决于传递参数的值或存储在object中的年份值。

Perhaps, you may want to define a function that returns true/false depending on the value of the passed parameter or the value of the year stored in object.

代码可能看起来像这个:

The code may look something like this:

leap.setLeapYear(2010);    // leap.leapYear = 2010;
System.out.println(leap.isLeapYear());

leap.setLeapYear(2008);    // leap.leapYear = 2008;
System.out.println(leap.isLeapYear());
leap.setLeapYear(1900);    // leap.leapYear = 1900;
System.out.println(leap.isLeapYear());
leap.setLeapYear(2000);
System.out.println(leap.isLeapYear());
leap.setLeapYear(1565);
System.out.println(leap.isLeapYear());

您必须通过将main中的检查移动到该函数来定义isLeapYear()。

You have to define isLeapYear() by moving the checks in main to that function.

这篇关于为什么我的程序只输出一个结果而不是五个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆