java01-08

Java 三百行狂练(01-08天,一些基本语法)

在大二上学期学习了Java这门课程,可是对于用java语言来编写代码掌握得还不够熟练,,所以从现在开始,重新开启java学习之旅,我是跟着大佬的代码进行练习的。

第1天:环境搭建
1.1无非需要两样东西,eclipse和jdk(或jre)
1.2 我遇到的问题
1.3 安装步骤
1.4第一段java代码"Hello World!"
#1.2

安装eclipse时,进度条不动,还提示下载的很慢(有这样的提示artifact download is progressing very slowly from https://ftp.jaist.ac.jp)
进度条不动,并且提示下载慢
解决办法
在下载的时候选择其他的镜像,默认是Japan,这时候点击Select Another Mirror,选择带China字样的进行下载
选择其他镜像
选择合适的镜像

#1.3

这里是主要的安装步骤
进入eclipse官网 https://www.eclipse.org,找到需要的eclipse软件,选择下载安装包,点击下载
找到Eclipse下载安装包
选择Eclipse IDE for Java Developers安装包
选择的是Eclipse IDE for Java Developers, 注意这里一定要选择合适的镜像(Select Another Mirror),否则可能会遇到我上面所写的问题。
选择其他镜像选择China的mirror最后还需要安装jdk,因为eclipse本身是用Java语言写的,所以需要安装jdk,提供一个java环境,进入java官网https://www.oracle.com进行下载
找到jdk

#1.4

代码如下:

package work;

/**
 * first code
 *@author yanfang
 */
public class HelloWorld {
	public static void main(String args[]) {
		System.out.println("Hello World!");
	}
}

运行结果为

Hello World!

第2天:基本算数操作

代码如下:

package work;

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

运行结果为

15 + 4 = 19
1.2 + 3.5 = 4.7
15 - 4 = 11
1.2 - 3.5 = -2.3
15 * 4 = 60
1.2 * 3.5 = 4.2
15 / 4 = 3
1.2 / 3.5 = 0.34285714285714286
15 % 4 = 3

加、减、乘、除、整除、取余
整数与整数运行的结果仍是整数。
好的代码本身就是注释,减少不必要的注释,命名要选择可读性强的名字。
变量定义的一般形式是 <类型名称> <变量名称>; int tempFirstInt;
变量的名字是一种标识符,标识符只能由字母,数字,下划线组成,且数字不能出现在第一个位置,java关键字不能做标识符。
变量赋值 tempFirstInt =15;将15赋给tempFirstInt;“=”是赋值运算符,将“=”左边的值赋给右边。
System.out.println()里的不加“”的+是字符串连接运算,而“+”里面的+则是字符。

第3天:基本 if 语句

代码如下:

package work;

/**
 * the usage of the if statement
 * @author yanfang
 *
 */
public class IfStatement {
	public static void main(String args[]) {
		int tempNumber1, tempNumber2;
		
		//try a positive value
		tempNumber1 = 5;
		if(tempNumber1 >= 0) {
			tempNumber2 = tempNumber1;
		}
		else {
			tempNumber2 = -tempNumber1;
		}
		
		System.out.println("The absolute value of " + tempNumber1 +" is " + tempNumber2);
		
		//try a negative value
		tempNumber1 = -3;
		if(tempNumber1 >= 0) {
			tempNumber2 = tempNumber1;
		}
		else {
			tempNumber2 = -tempNumber1;
		}
		
		System.out.println("The absolute value of " + tempNumber1 +" is " + tempNumber2);
		
		//use a method/function to achieve
		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));
	}
	//found this method/function
	public static int abs(int paraValue) {
		if(paraValue >=0) {
			return paraValue;
		}
		else {
			return -paraValue;
		}
	}
}

运行结果为

The absolute value of 5 is 5
The absolute value of -3 is 3
The absolute value of 6 is 6
The absolute value of -8 is 8

计算一个数的绝对值。可以直接利用if…else语句解决,也可以创建一个方法或函数来求。

第4天:闰年计算

代码如下:

package work;

/**
 * use if statement to confirm LeapYear
 * @author yanfang
 */
public class LeapYear {
	public static void main(String args[]) {
		//test isLeapYear
		//if 2021
		int tempYear = 2021;
		System.out.println(""+ tempYear + " is");
		if(!isLeapYear(tempYear)) {
			System.out.print("NOT ");
		}
		System.out.println("a leap year.");
		
		//if 2000
		tempYear = 2000;
		System.out.println(""+ tempYear + " is");
		if(!isLeapYear(tempYear)) {
			System.out.print("NOT ");
		}
		System.out.println("a leap year.");
		
		//if 2100
		tempYear=2100;
		System.out.println(""+ tempYear + " is");
		if(!isLeapYear(tempYear)) {
			System.out.print("NOT ");
		}
		System.out.println("a leap year.");
		
		//if 2004
		tempYear= 2004;
		System.out.println(""+ tempYear + " is");
		if(!isLeapYear(tempYear)) {
			System.out.print("NOT ");
		}
		System.out.println("a leap year.");
		
		//test isLeapYearV2
		System.out.println("Now use the second version");
		//if 2021
		 tempYear = 2021;
		System.out.println(""+ tempYear + " is");
		if(!isLeapYearV2(tempYear)) {
			System.out.print("NOT ");
		}
		System.out.println("a leap year.");
		
		//if 2000
		tempYear = 2000;
		System.out.println(""+ tempYear + " is");
		if(!isLeapYearV2(tempYear)) {
			System.out.print("NOT ");
		}
		System.out.println("a leap year.");
		
		//if 2100
		tempYear=2100;
		System.out.println(""+ tempYear + " is");
		if(!isLeapYearV2(tempYear)) {
			System.out.print("NOT ");
		}
		System.out.println("a leap year.");
		
		//if 2004
		tempYear= 2004;
		System.out.println(""+ tempYear + " is");
		if(!isLeapYear(tempYear)) {
			System.out.print("NOT ");
		}
		System.out.println("a leap year.");
	}//main
	// found isLeapYear
	public static boolean isLeapYear(int paraYear) {
		if((paraYear%4 == 0)&&(paraYear % 100 !=0)||(paraYear %400 ==0)){
			return true;
		}else {
			return false;
		}
	}
	//found isLeapYearV2
	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;
		}
	}
}

运行结果为

2021 is
NOT a leap year.
2000 is
a leap year.
2100 is
NOT a leap year.
2004 is
a leap year.
Now use the second version
2021 is
NOT a leap year.
2000 is
a leap year.
2100 is
NOT a leap year.
2004 is
a leap year.

判断闰年,若年份能被4整除,且不能被100整除,或能被400整除,则是闰年。可以采用级联的 if-else if 语句完成判断

第5天:基本swith语句

代码如下:

package work;

/**
 * about the use of SwithStatement
 * @author yanfang
 */
public class SwithStatement {
	public static void main(String args[]) {
		scoreToLevelTest();
	}
	
	public static char scoreToLevelTest(int paraScore) {
		//E stands for error, and F stands for fail
		char resultLevel = 'E';
		
		//Divide by 10, the result ranges form 0 to 10
		int tempDigitalLevel = paraScore / 10;
		
		//The use of break is important
		switch (tempDigitalLevel) {
		case 10:
		case 9: resultLevel = 'A';break;//10,9执行相同代码
		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';
		}
		return resultLevel;
	}
	public static void scoreToLevelTest() {
		int tempScore = 100;
		System.out.println("Score " + tempScore +" to level is: " +scoreToLevelTest(tempScore));
		
		tempScore = 91;
		System.out.println("Score " + tempScore +" to level is: " +scoreToLevelTest(tempScore));
		
		tempScore = 82;
		System.out.println("Score " + tempScore +" to level is: " +scoreToLevelTest(tempScore));
		
		tempScore = 75;
		System.out.println("Score " + tempScore +" to level is: " +scoreToLevelTest(tempScore));
		
		tempScore = 66;
		System.out.println("Score " + tempScore +" to level is: " +scoreToLevelTest(tempScore));
		
		tempScore = 52;
		System.out.println("Score " + tempScore +" to level is: " +scoreToLevelTest(tempScore));
		
		tempScore = 8;
		System.out.println("Score " + tempScore +" to level is: " +scoreToLevelTest(tempScore));
		
		tempScore = 120;
		System.out.println("Score " + tempScore +" to level is: " +scoreToLevelTest(tempScore));
	}
}

运行结果为

Score 100 to level is: A
Score 91 to level is: A
Score 82 to level is: B
Score 75 to level is: C
Score 66 to level is: D
Score 52 to level is: F
Score 8 to level is: F
Score 120 to level is: E

利用swith语句来进行成绩判定,其基本结构为

/*swith(控制表达式){
case 常量1:语句1; break;
case 常量2:语句2; break;
case 常量n:语句n; break;
default:语句;
}
*/

计算出控制表达式的值后,程序跳转到匹配的case处并执行相应语句,若所有case都不匹配,则执行default里的语句,若没有default则什么都不做。若分支的语句后没有break,会顺序执行后面的case,直到遇到break或swich结束

第6天:基本for语句

代码如下:

package work;

/**
 * the sixth code
 * @author yanfang
 */
public class ForStatement {
	public static void main(String arge[]) {
		forStatementTest();
	}
	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));
	}
	public static int addToN(int paraN) {
		int resultSum = 0;
		for(int i =1; i <=paraN; i++) {
			resultSum += i;
		}
		return resultSum;
	}
	public static int addToNWithStepLength(int paraN,int paraStepLength) {
		int resultSum = 0;
		for(int i =1; i<=paraN; i+=paraStepLength) {
			resultSum += i;
		}
		return resultSum;
	}
}

运行结果为

1 add to 10 is: 55
1 add to 0 is: 0
1 add to 10 with step length 1 is: 55
1 add to 10 with step length 2 is: 25

利用for循环语句进行从1到10的数据求和计算,还可以计算数的阶乘。求和运算,变量i初始化为0,求积运算,变量i初始化为1。

第7天:矩阵元素相加

代码如下:

package work;

import java.util.Arrays;

public class MatrixAddition {
	public static void main(String args[]) {
		matrixElementSumTest();
		matrixAdditionTest();
	}
	
	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] ;
			}
		}
		return resultSum;
	}
	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;//这里给出矩阵中元素的取值方法
			}
		}
		System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
		System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
	}
	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];// paraMatrix2为全0的矩阵
		}
		}
		return resultMatrix;
	}
	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;
		}
	}
		System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
}
	
}


运行结果为

The matrix is: 
[[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]]
The matrix element sum is: 138

The matrix is: 
[[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]]

用数组存储矩阵,用for语句的二重循环实现矩阵。
Arrays.deepToString(数组名)用于数组中还有数组的情况,输出数组结果。

第8天:矩阵相乘

代码如下:

package work;

import java.util.Arrays;

/**
 * the eighth code
 * @author yanfang
 */
public class MatrixMultiplication {
	public static void main(String args[]) {
		matrixMultiplicationTest();
	}
	
	public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
		int m = paraFirstMatrix.length;//第一个矩阵的行数
		int n = paraFirstMatrix[0].length;//第一个矩阵的列数
		int p = paraSecondMatrix[0].length;//第二个矩阵的列数
		
		//step1 dimension check
		if(paraSecondMatrix.length != n) {
			System.out.println("The two matrices cannot be multiplied.");
			return null;//判断两个矩阵能否相乘
		}
		//step2 the loop
		int[][] resultMatrix = new int[m][p];
		for(int i = 0; i < m; i++) {
			for(int j = 0; j < p; j++) {
				for(int k = 0; k < n; k++) {
					resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];//相加的项数为第一个矩阵的列数
				}
			}
		}
	
		return resultMatrix;
	}
	public static void matrixMultiplicationTest() {
		int[][] tempFirstMatrix = new int[2][3];
		for(int i = 0; i<tempFirstMatrix.length; i++) {
			for (int j = 0; j<tempFirstMatrix[0].length; j++) {
				tempFirstMatrix[i][j] = i+j;
			}
		}
		System.out.println("The first matrix is : \r\n" + Arrays.deepToString(tempFirstMatrix));
		
		int[][] tempSecondMatrix = new int[3][2];
		for(int i = 0; i < tempSecondMatrix.length; i++) {
			for(int j = 0; j < tempSecondMatrix[0].length; j++) {
				tempSecondMatrix[i][j] = i*10 +j;
			}
		}
		System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempSecondMatrix));
		
		int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
		System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
		
		System.out.println("Trying to multiply the first matrix with itself.\r\n");
		tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
		System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
 	}
	
}

运行结果为

The first matrix is : 
[[0, 1, 2], [1, 2, 3]]
The second matrix is: 
[[0, 1], [10, 11], [20, 21]]
The third matrix is: 
[[50, 53], [80, 86]]
Trying to multiply the first matrix with itself.

The two matrices cannot be multiplied.
The result matrix is: 
null

第一个矩阵的列数(column)和第二个矩阵的行数(row)相同,两个矩阵可以相乘。结果矩阵的行数等于第一个矩阵的行数,列数等于第二个矩阵的列数
矩阵相乘
利用for的三重循环实现计算,我这里不是很理解

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值