[英]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?
我该怎么做呢?
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));
...
leapYear
is static
, so you don't need to / shouldn't do LeapYear leap = new LeapYear();
leapYear
是static
,所以你不需要/不应该做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.