日撸 Java 三百行第一周(01-07天,环境搭建与基本语法)

注意:这是自己java的学习过程的记录,如有问题欢迎指正。


一、day01:环境搭建

环境搭建的话建议上b站,有很多具体的步骤视频可以找到。环境搭建与eclipse的安装视频

掌握package、import、println语句如何使用。注意package 要与所建的包名一致。

package basic;

/**
 * This is the first code. 
 * @author Xuanlin Zhu 1306805091@qq.com. 
 */

public class HelloWorld {

	public static void main(String args[]) {
		System.out.println("Hello, world!");
	}//Of main
}//Of class HelloWorld

print、println、printf的区别:print输出后不提行,println输出后会在结尾插入换行符,而printf则是格式化输出的形式。

二、day02:基本算术操作

代码如下(示例):

package basic;

/**
 * This is the second code.
 * @author Xuanlin Zhu 1306805091@qq.com.
 */
public class BasicOperations {

	public static void main(String args[]) {
		int tempFirstInt, tempSecondInt, tempResultInt;
		double tempFirstDouble, tempSecondDouble, tempResultDouble;
		
		tempFirstInt = 15;
		tempSecondInt = 4;
		
		tempFirstDouble = 1.2;
		tempSecondDouble = 3.5;
		
		//Addition
		tempResultInt = tempFirstInt + tempSecondInt;
		tempResultDouble = tempFirstDouble + tempSecondDouble;
		
		System.out.println("" + tempFirstInt + " + " + tempSecondInt + " = " + tempResultInt);
		System.out.println("" + tempFirstDouble + " + " + tempSecondDouble + " = " + tempResultDouble);
		
		//Subtraction
		tempResultInt = tempFirstInt - tempSecondInt;
		tempResultDouble = tempFirstDouble - tempSecondDouble;
		
		System.out.println("" + tempFirstInt + " - " + tempSecondInt + " = " + tempResultInt);
		System.out.println("" + tempFirstDouble + " - " + tempSecondDouble + " = " + tempResultDouble);
		
		//Multiplication
		tempResultInt = tempFirstInt * tempSecondInt;
		tempResultDouble = tempFirstDouble * tempSecondDouble;
		
		System.out.println("" + tempFirstInt + " * " + tempSecondInt + " = " + tempResultInt);
		System.out.println("" + tempFirstDouble + " * " + tempSecondDouble + " = " + tempResultDouble);
		
		//Division
		tempResultInt = tempFirstInt / tempSecondInt;
		tempResultDouble = tempFirstDouble / tempSecondDouble;
		
		System.out.println("" + tempFirstInt + " / " + tempSecondInt + " = " + tempResultInt);
		System.out.println("" + tempFirstDouble + " / " + tempSecondDouble + " = " + tempResultDouble);
		
		//Modulus
		tempResultInt = tempFirstInt % tempSecondInt;
		
		System.out.println("" + tempFirstInt + " % " + tempSecondInt + " = " + tempResultInt);
	}//Of main
}//Of class BasicOperations

java语言的部分规范:

1.项目名和类名全部小写。

2.类名首字母大写,若类名由多个单词构成,每个单词首字母大写,即大驼峰命名法

3.变量名、方法名首字母小写,若其由多个单词构成,每个单词首字母大写,即小驼峰命名法

4.常量名全部大写。

5.注释分为文档注释(/** */ )、行注释(//) 、块注释(/* */ )三种。

深入了解java语言中的命名规范与注释规范请浏览:java的命名规范与注释规范。

(注意整除和取余的差别,不要混淆。)

三、day03:基本if 语句

package basic;

/**
 * This is the third code.
 * @author Xuanlin Zhu 1306805091@qq.com.
 */

public class IfStatement {

	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		int tempNumber1, tempNumber2;

		// Try a positive value
		tempNumber1 = 5;

		if (tempNumber1 >= 0) {
			tempNumber2 = tempNumber1;
		} else {
			tempNumber2 = -tempNumber1;
		} // Of if

		System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);

		// Try a negative value
		// Lines 27 through 33 are the same as Lines 15 through 19
		tempNumber1 = -3;

		if (tempNumber1 >= 0) {
			tempNumber2 = tempNumber1;
		} else {
			tempNumber2 = -tempNumber1;
		} // Of if

		System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);

		// Now we use a method/function for this purpose.
		tempNumber1 = 6;
		System.out.println("The absolute value of " + tempNumber1 + " is " + abs(tempNumber1));
		tempNumber1 = -8;
		System.out.println("The absolute value of " + tempNumber1 + " is " + abs(tempNumber1));
	}// Of main

	/**
	 *********************
	 * The absolute value of the given parameter.
	 * 
	 * @param paraValue The given value.
	 *********************
	 */
	public static int abs(int paraValue) {
		if (paraValue >= 0) {
			return paraValue;
		} else {
			return -paraValue;
		} // Of if
	}// Of abs
}// Of class IfStatement

if语句的使用方式:

if(布尔型表达式)

{

语句;

}

else

{

语句;

}

更多关于if条件语句请浏览:java if语句


运行结果:

四、day04:闰年的计算(if语句的嵌套使用)

package basic;

/**
 * This is the forth code.
 * @author Xuanlin Zhu 1306805091@qq.com. 
 */

public class LeapYear {

	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 *********************
	 */
	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 = 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

闰年:闰年分为普通闰年和世纪闰年

普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。

世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)。

五、day05:基本switch 语句

package basic;

/**
 * This is the fifth code.
 * @author Xuanlin Zhu 1306805091@qq.com.
 */
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';
		}// 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));
	}// Of scoreToLevelTest

}// Of class SwitchStatement

1.switch 语句由一个控制表达式和多个case标签组成。

2.能用在switch上的类型有,char ,byte,short,int以及相应的包装类型。

3.witch与if类似,但通常来说,switch语句效率更高。

4.default会在当前switch找不到匹配的case时执行。default并不是必须的。一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break。

六、day06:基本for语句

package basic;

/**
 * This is the sixth code.
 * @author Xuanlin Zhu 1306805091@qq.com.
 */
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 = 0;
		System.out.println("1 add to " + tempN + " is: " + addToN(tempN));

		int tempStepLength = 1;
		tempN = 10;
		System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
				+ addToNWithStepLength(tempN, tempStepLength));

		tempStepLength = 2;
		System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
				+ addToNWithStepLength(tempN, tempStepLength));
	}// 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;表达式2;表达式3)

{

语句块;

}

表达式1为赋值语句,如int i=1;

表达式2为条件语句,如i<=10;

表达式3为迭代语句,如i++;i--;

七、day07:矩阵元素相加

package basic;

import java.util.Arrays;


/**
 * This is the seventh code.
 * @author Xuanlin Zhu 1306805091@qq.com. 
 */

public class MatrixAddition {

	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		matrixElementSumTest();

		matrixAdditionTest();
	}// 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

	/**
	 *********************
	 * Add two matrices. Attention: NO error check is provided at this moment.
	 * 
	 * @param paraMatrix1 The first matrix.
	 * @param paraMatrix2 The second matrix. It should have the same size as
	 *                    the first one's.
	 * @return The addition of these matrices.
	 *********************
	 */
	public static int[][] matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2) {
		int[][] resultMatrix = new int[paraMatrix1.length][paraMatrix1[0].length];

		for (int i = 0; i < paraMatrix1.length; i++) {
			for (int j = 0; j < paraMatrix1[0].length; j++) {
				resultMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j];
			} // Of for j
		} // Of for i

		return resultMatrix;
	}// Of matrixAddition

	/**
	 *********************
	 * Unit test for respective method.
	 *********************
	 */
	public static void matrixAdditionTest() {
		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));
		int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
		System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
	}// Of matrixAdditionTest

}// Of class MatrixAddition

当我们需要使用数组时,应该先引入,import java.util.Arrays;

数组的初始化:

数组类型[](若为二维数组,则需要写两个中括号) 数组名字=new 数组类型[长度1][长度2];

array.length表示的时数组行数,array.length[0]表示的是数组的列数。

总结

通过第一周的学习,让我发现了自身的许多不足,也了解到了如何规范命名以及注释文档的要求,同时认识到计算机编程是一个非常严谨的事情,需要我们在每一个地方都做好,特别是细节问题,print和println的区别就是一个很好的例子。同时还认识到编程的实质就是首先需要我们有一个清晰的思路,然后再将我们的想法转化成为计算机语言。对程序的优化就是对思路的优化,对解决问题的方法的优化。在今后的学习过程中,更应该努力提高自己的逻辑能力以及解决问题的速度,不仅追求解决问题,更应该追求更快的解决问题。同时也要注意格式的规范性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值