欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

日撸代码300行学习笔记Day4

时间:2023-07-10
1.Day 4:实现闰年的计算

 在闵老师的代码中,不难发现在其中一部分if语句中,嵌套了定义的boolean类型的isLeapYear以及isLeapYearV2函数,对其进行调用。在第一种函数中,采用逻辑运算符 && 和 || 在进行判别,返回true或false。第二种是采用if语句进行计算判别。并且在输出时,换成了print,可以使先后不同的输出语句在同一排显示,如果需要提行,则换成了println。

package demo;public class Demo1 {public static void main(String[] args) {// Test isLeapYearint tempYear = 2021;System.out.print("" + tempYear + " is ");if (!isLeapYear(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2000;System.out.print("" + tempYear + " is ");if (!isLeapYear(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2100;System.out.print("" + tempYear + " is ");if (!isLeapYear(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2004;System.out.print("" + tempYear + " is ");if (!isLeapYear(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");// Test isLeapYearV2System.out.println("Now use the second version.");tempYear = 2023;System.out.print("" + tempYear + " is ");if (!isLeapYearV2(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2006;System.out.print("" + tempYear + " is ");if (!isLeapYearV2(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2105;System.out.print("" + tempYear + " is ");if (!isLeapYearV2(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");tempYear = 2004;System.out.print("" + tempYear + " is ");if (!isLeapYearV2(tempYear)) {System.out.print("NOT ");} // Of ifSystem.out.println("a leap year.");}// Of mainpublic static boolean isLeapYear(int paraYear) {if ((paraYear % 4 == 0) && (paraYear % 100 != 0) || (paraYear % 400 == 0)) {return true;} else {return false;} // Of if}// Of isLeapYearpublic 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中,等号两边加不加空格好像都不影响程序的运行,那我们平常在写的时候,是否是要加上这种空格喃?

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。