日撸 Java 三百行 (04-05,基础语法)

第四天:闰年的计算

1.if语句的嵌套

2.闰年的基本运算规律

3.布尔类型

package basic;/** * The complex usage of the if statement. *  * @author 凤fff * */public class LeapYear {/** ************************** * The entrance of the program. *  * @param args Not used now. ************************** */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 = 2021 ;System.out.print(" " + tempYear + " is ");if ( ! isLeapYearV2(tempYear)) {System.out.print("Not ");} // Of if System.out.println("a leap year.") ;tempYear = 2000 ;System.out.print(" " + tempYear + " is ");if ( ! isLeapYearV2(tempYear)) {System.out.print("Not ");} // Of if System.out.println("a leap year.") ;tempYear = 2100 ;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/** *********************** * Is the given year leap? *  * @param paraYear The given year. *********************** */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/** *********************** * Is the given year leap? Replace the complex condition with a number of if. * * @param paraYear The given Year. *********************** */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} // Of class LeapYear

运行结果

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUGhvZW5peCBrbmlnaHQ=,size_13,color_FFFFFF,t_70,g_se,x_16

注意,在 isLeapYearV2 中 取余100 和取余 400 不能交换位置 判断是从上向下执行的如果上面的满足条件就不会向下执行

第五天:

1.Switch, case, break, default 用法

2. 单元测试单独使用一个方法,main 方法里面的代码越少越好

package basic;/** * This is the fifth code. Names and comments should follow my style strictly. *  * @author 凤fff * */public class SwitchStatement {/** ********************* * The entrance of the program. *  * @param args *            Not used now. ********************* */public static void main(String args[]) {scoreToLeveTest();} // Of main/** ********************* * Score to level. *  * @param paraScore *            From 0 to 100. * @return The level form A to F. ********************* */public static char scoreToLevel(int paraScore) {// E stand for error, and F stands for fail.char resultLevel = 'E';// Divide by 10, the result rangs from 0 to 10int tempDigitalLevel = paraScore / 10;// The use of break is important.switch (tempDigitalLevel) {case 10:case 9:resultLevel = 'A';break;case 8:resultLevel = 'B';break;case 7:resultLevel = 'C';break;case 6:resultLevel = 'D';break;case 5:case 4:case 3:case 2:case 1:case 0:resultLevel = 'F';break;default:resultLevel = 'E';} // Of switchreturn resultLevel;} // Of scoreToLevelpublic static void scoreToLeveTest() {int tempscore = 100;System.out.println("Score " + tempscore + " to level is: " + scoreToLevel(tempscore));tempscore = 91;System.out.println("Score " + tempscore + " to level is: " + scoreToLevel(tempscore));tempscore = 83;System.out.println("Score " + tempscore + " to level is: " + scoreToLevel(tempscore));tempscore = 75;System.out.println("Score " + tempscore + " to level is: " + scoreToLevel(tempscore));tempscore = 66;System.out.println("Score " + tempscore + " to level is: " + scoreToLevel(tempscore));tempscore = 53;System.out.println("Score " + tempscore + " to level is: " + scoreToLevel(tempscore));tempscore = 8;System.out.println("Score " + tempscore + " to level is: " + scoreToLevel(tempscore));tempscore = 120;System.out.println("Score " + tempscore + " to level is: " + scoreToLevel(tempscore));} // Of scoreToLevelTest} // Of class SwitchStatement

运行结果

 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUGhvZW5peCBrbmlnaHQ=,size_10,color_FFFFFF,t_70,g_se,x_16

条件分支有多个 条件的值是整数或一个字符值 选用Switch更好

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值