1.Day 4:实现闰年的计算
?在闵老师的代码中,不难发现在其中一部分if语句中,嵌套了定义的boolean类型的isLeapYear以及isLeapYearV2函数,对其进行调用。在第一种函数中,采用逻辑运算符 && 和 || 在进行判别,返回true或false。第二种是采用if语句进行计算判别。并且在输出时,换成了print,可以使先后不同的输出语句在同一排显示,如果需要提行,则换成了println。
package demo;
public class Demo1 {
public static void main(String[] args) {
// Test isLeapYear
int tempYear = 2021;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
tempYear = 2000;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
tempYear = 2100;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
tempYear = 2004;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
// Test isLeapYearV2
System.out.println("Now use the second version.");
tempYear = 2023;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
tempYear = 2006;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
tempYear = 2105;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
tempYear = 2004;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
}// Of main
public static boolean isLeapYear(int paraYear) {
if ((paraYear % 4 == 0) && (paraYear % 100 != 0) || (paraYear % 400 == 0)) {
return true;
} else {
return false;
} // Of if
}// Of isLeapYear
public static boolean isLeapYearV2(int paraYear) {
if (paraYear % 4 != 0) {
return false;
} else if (paraYear % 400 == 0) {
return true;
} else if (paraYear % 100 == 0) {
return false;
} else {
return true;
} // Of if
}// Of isLeapYearV2
}
Part 1结果:
?Part 2结果:
?2.Question
在赋值中,例如? int num=1和 int num = 1中,等号两边加不加空格好像都不影响程序的运行,那我们平常在写的时候,是否是要加上这种空格喃?
|