简体   繁体   English

为什么我的程序只输出一个结果而不是五个?

[英]Why does my program only outputs one result instead five?

/*
 *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.");
        }
    }
}

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.我是 Java 的新手,我编写了这段代码,以便它可以调用 boolean 的所有五年并为所有这些生成答案。 However it only calls the last one.但是它只调用最后一个。 How would I do this right?我该怎么做呢?

Problem 1问题 1

The four first calls to setLeapYear :第四个调用setLeapYear

leap.setLeapYear(2010);    // leap.leapYear = 2010;
leap.setLeapYear(2008);    // leap.leapYear = 2008;
leap.setLeapYear(1900);    // leap.leapYear = 1900;
leap.setLeapYear(2000);    // leap.leapYear = 2000;

gets overridden by the last one :被最后一个覆盖

leap.setLeapYear(1565);    // leap.leapYear = 1565;

I would probably write a boolean method called something like isLeapYear(int year) and do我可能会写一个 boolean 方法,称为isLeapYear(int year)并执行

System.out.println(isLeapYear(2010));
System.out.println(isLeapYear(2008)); 
...


Problem 2问题 2

leapYear is static , so you don't need to / shouldn't do LeapYear leap = new LeapYear(); leapYearstatic ,所以你不需要/不应该做LeapYear leap = new LeapYear(); (alternatively, you should drop the static modifier). (或者,您应该删除static修饰符)。

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.您需要为每一年使用单独的 object,或者至少在为那一年创建 object 后立即调用闰年检查方法。

What you have is a series of call to a function that assigns a value to an attribute of the same object.您所拥有的是对 function 的一系列调用,该调用将值分配给同一 object 的属性。 Therefore, only the last statement has the effect as previous values are overwritten.因此,只有最后一条语句具有覆盖先前值的效果。

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.您是否在 Main 中进行了检查,似乎leapYear没有在任何地方定义。

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.也许,您可能想要定义一个 function,它根据传递的参数值或 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());

You have to define isLeapYear() by moving the checks in main to that function.您必须通过将 main 中的检查移动到 function 来定义 isLeapYear()。

I would be inclined to write the tests in your if statements in order from 400 to 4:我倾向于在您的if语句中按 400 到 4 的顺序编写测试:

String result;
if (year % 400 == 0) {
    result = "is a leap year.";
} else if (year % 100 == 0) {
    result = "is not a leap year.";
} else if (year % 4 == 0) {
    result = "is a leap year.";
} else {
    result = "is not a leap year.";
}
System.out.println(year + ": " + result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初学者休眠:为什么我的休眠查询只返回一个结果,而不是列表? - Beginner hibernate: Why does my hibernate query return only one result, instead of a list? 为什么我的程序只显示我的数组之一<string>我的所有 hashmap 数据中的食物而不是实际值?</string> - Why does my program only show one of my Array <String> Food in all of my hashmap data instead of the actual values? "<i>Why does my while loop only go through one name in the database, instead of all of them?<\/i>为什么我的 while 循环只遍历数据库中的一个名称,而不是所有名称?<\/b> <i>my program should sum values assigned to the same name<\/i>我的程序应该对分配给同名的值求和<\/b>" - Why does my while loop only go through one name in the database, instead of all of them? my program should sum values assigned to the same name 为什么我的程序要求输入一个符号两次而不是一个(忽略第一个符号)? - Why does my program ask for a symbol 2 times instead of one(ignores first symbol)? 为什么我的程序仅返回零? - Why does my program return zeros only? Mapreduce程序仅输出一条记录 - Mapreduce program outputs only one record Java:尝试将我的代码打印在一行而不是五行上 - Java: Trying to get my code to print on one line instead of five 为什么我的 XML 解析器只返回一个字符串,而不是多个? - Why does my XML parser only returns one string, instead of multiple ones? 为什么程序打印高度值0而不是我设置的高度值? - Why does the program print the height value 0 instead of the one I set? 程序从多种方法而不是一种方法输出代码 - Program outputs code from multiple methods instead of just one method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM