日撸代码300行:4-7天

代码来自闵老师”日撸Java三百行(01-10天,基本语法),原文链接:“https://blog.csdn.net/minfanphd/article/details/116933803
闵老师教程刚开始的时候有跟着学习,当时跟着坚持到第七天。今天对四、五、六、七四天的学习进行一个总结,接下来将从第八天继续学习。

第四天:闰年的计算

package basic;


/**
 * The complex usage of the if statement. The names and comments are strictly in the original article format
 * 
 * @author WX873
 *
 */

public class LeapYear {
	/**
	 * *************************** The entrance of the program.
	 * 
	 * @param args
	 *            Not used now. ***************************
	 */

	public static void main(String args[]) {
		// Test isLeapYear
		int tempYear = 2000;
		System.out.println(tempYear + " is");
		if (!isLeapYear(tempYear)) {
			System.out.println("NOT");
		} // of if
		System.out.println("a leap year");

		tempYear = 2100;
		System.out.println(tempYear + " is");
		if (!isLeapYear(tempYear)) {
			System.out.println("NOT");
		} // of if
		System.out.println("a leap year");

		tempYear = 2020;
		System.out.println(tempYear + " is");
		if (!isLeapYearV2(tempYear)) {
			System.out.println("NOT");
		} // of if
		System.out.println("a leap year");

	}// of main

	public static boolean isLeapYear(int paraYear) {
		if ((paraYear % 400 == 0) || (paraYear % 100 != 0) && (paraYear % 4 == 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 isLeapYear

}// of class

今天主要是if语句的使用,以及if语句的嵌套使用。if语句主要用于判断,布尔类型的返回值为0和1。今天的代码用两种方式实现了闰年的判断,一种是通过逻辑运算符,一种是通过if语句的嵌套。
逻辑运算符已经忘的差不多了,急忙百度了一下。(逻辑运算符详细介绍:http://c.biancheng.net/view/777.html)&&和&都可以表示逻辑与,但是有区别。&&又称短路与(个人觉得这个说法很有意思),&称为逻辑与。&&(短路与)只要第一个条件不成立,就不会再去判断第二个条件,直接返回false,而&会判断所有的条件。||和|的区别也类似,||只要满足一个条件,后面就不再判断了。

第五天:基本switch 语句

package basic;

/**
 * This is the fifth code. The names and comments are strictly in the original article format.
 * 
 * @author WX873
 *
 */

public class SwitchStatement {
	/**
	 * *************************************
	 * The entrance of the program
	 * 
	 * @param args Not used now
	 * *************************************
	 */
	public static void main(String args[]) {
		scoreToLevelTest();
		
	}//of main
	
	/**
	 * **************************************
	 * Score to level
	 * 
	 * @param paraScore From 0 to 100.
	 * return The level from A to F.
	 * **************************************
	 */
	public static char scoreToLevel(int paraScore) {
		//E stands for error, and F stands for fail.
		char resultLevel = 'E';
		
		//Divide by 10, the result ranges from 0 to 10.
		int 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';
			break;
		}//of switch
		return resultLevel;
	}//of scoreToLevel
	
	/**
	 * *****************************************
	 * Method unit test.
	 * *****************************************
	 */
	public static void scoreToLevelTest() {
		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 = 82;
		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 = 52;
		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));
		
		tempScore = -66;
		System.out.println("Score " + tempScore + " to level is:" + scoreToLevel(tempScore));
	}//of scoreToLevelTest

}//of class SwitchStatement

switch语句经常用来解决多种分支的情况。switch 语句提供了 if 语句的一个变通形式,可以从多个语句块中选择其中的一个执行。switch的详细介绍见:http://c.biancheng.net/view/738.html
default表示“默认”,就是说case所列情况都不满足,执行该分支。default 后要紧跟冒号,default 块和 case 块的先后顺序可以变动,不会影响程序执行结果。通常,default 块放在末尾,也可以省略不写。

第六天:基本for 语句

package basic;

/**
 * 
 * This is the sixth code. The names and comments are strictly in the original article format.
 * 
 * @author WX873
 *
 */

public class ForStatement {

	/**
	 * ********************************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 * ********************************
	 */
	
	public static void main(String args[]) {
		forStatementTest();
	}//of main
	
	/**
	 * ****************************
	 * Method unit test
	 * ****************************
	 */
	public static void forStatementTest() {
		int tempN = 10;
		System.out.println("1 add to " + tempN + " is:" + addToN(tempN));
		
		tempN = 100;
		System.out.println("1 add to " + tempN + " is:" + addToN(tempN));
		
		tempN = 0;
		System.out.println("1 add to " + tempN + " is:" + addToN(tempN));
		
		tempN = 10;
		int tempStepLenth = 1;
		System.out.println("1 add to " + tempN + " is:" + addToNWithStepLength(tempN, tempStepLenth));
		
		tempN = 10;
		tempStepLenth = 2;
		System.out.println("1 add to " + tempN + " is:" + addToNWithStepLength(tempN, tempStepLenth));
		
		tempN = 101;
		tempStepLenth = 4;
		System.out.println("1 add to " + tempN + " is:" + addToNWithStepLength(tempN, tempStepLenth));
		
	}//of forStatementTest
	
	/**
	 * **************************************
	 * Add from 1 to N
	 * 
	 * @param paraN The given upper bound.
	 * @return The sum
	 * **************************************
	 */
	public static int addToN(int paraN) {
		int resultSum = 0;
		for (int i = 1; i <= paraN; i++) {
			resultSum += i; 
		}//of for i
		return resultSum;
	}//of addToN
	
	/**
	 * **********************************
	 * Add from 1 to N with a step length.
	 * 
	 * @param paraN               The given upper bound.
	 * @param paraStepLength      The given step length.
	 * @return The sum.
	 */
	public static int addToNWithStepLength(int paraN, int paraStepLength) {
		int resultSum = 0;
		for (int i = 1; i <= paraN; i += paraStepLength) {
			resultSum += i;
		}//of for i
		return resultSum;
	}//of addToNWithStepLength
}//of class ForStatement

for语句是程序中常用的语句,也是程序的核心,算法的时间复杂度一般根据循环语句来计算。今天的代码用for语句实现了1累加到N,和变步长1加到N两个功能。addToNWithStepLength(int paraN, int paraStepLength)方法是addToN(int paraN)的一般形式,如果步长paraStepLength为1,那么两个方法功能相同。

第七天:矩阵元素相加

package basic;

import java.util.Arrays;

/***
 * This is the seventh code. The names and comments are strictly in the original article format. 
 * 
 * @author WX873
 *
 */
public class MatrixAddition {
	/**
	 * *******************************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 * *******************************
	 */
	public static void main(String srgs[]) {
		matrixElementSumTest();
	}//of main
	
	/**
	 * ******************************
	 * Sum the elements of a matrix.
	 * 
	 * @param paraMatrix The given matrix.
	 * @return The sum of all its elements.
	 * ******************************
	 */
	public static int matrixElementSum(int[][] paraMatrix) {
		int resultSum = 0;
		for (int i = 0; i < paraMatrix.length; i++) {
			for (int j = 0; j < paraMatrix[0].length; j++) {
				resultSum += paraMatrix[i][j];
			}//of for j
		}//of for i
		return resultSum;
	}//of matrixElementSum
	
	/**
	 * ********************************
	 * Unit test for respective method.
	 * ********************************
	 */
	public static void matrixElementSumTest() {
		int[][] tempMatrix = new int[3][4];
		for (int i = 0; i < tempMatrix.length; i++) {
			for (int j = 0; j < tempMatrix[0].length; j++) {
				tempMatrix[i][j] = i * 10 + j;
			}//of for j
		}//of for i
		System.out.println("The matrix is:\r\n" + Arrays.deepToString(tempMatrix));
		System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
	}//of matrixElementSumTest

}//of class MatrixAddition
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值