java学习

这篇博客记录了一位初学者从第一天开始学习Java的过程,包括环境搭建、基本语法、算术操作、条件判断(if、switch)、循环控制(for、while)、矩阵运算等。每天的学习内容逐步深入,涉及包管理、变量、运算符、流程控制语句、数组操作和矩阵乘法。通过实例展示了如何使用这些基础知识解决问题。
摘要由CSDN通过智能技术生成

 java学习第一天: 环境搭建

学习:

1.1 完成 Eclipse 的安装. 第一天嘛,把环境弄对就行了.
1.2 学习 package, import 和 println 语句. 其中, package 要与所建的包名(即文件夹名)一致.
1.3 编写HelloWorld.java. 一定要注意变量的写法.

代码输入:

package basic;

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

输出结果:

Hello, world!

 java学习第二天:基本算术操作

学习:

1 加、减、乘、除、整除、取余.
2 熟悉 println 的中阶用法.

遇到的问题:

通过观看教学视频学习得到个个运算符号代表的意义

代码输入:

package basic;

public class BasicOperations {
	public static void main(String args[]) {
		int tempFirstInt, tempSecondInt, tempResultInt;
		double tempFirstDouble, tempSecondDouble, tempResultDouble;
		
		tempFirstInt = 12;
		tempSecondInt = 5;
		
		tempFirstDouble = 1.2;
		tempSecondDouble = 4.6;
		
		//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

输出结果:

12 + 5 = 17
1.2 + 4.6 = 5.8
12 - 5 = 7
1.2 - 4.6 = -3.3999999999999995
12 * 5 = 60
1.2 * 4.6 = 5.52
12 / 5 = 2
1.2 / 4.6 = 0.2608695652173913
12 % 5 = 2

java学习第三天:基本if 语句

学习:

1 if then else.
2 方法(函数)调用: 增加代码的复用性.
3 方法(函数)头部规范的注释, 是后期生成文档的基础.

遇到的问题:

由于编程基础为零所以观看教学视频知道if语句的基本使用

 

代码输入:

package basic;

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 = 13;

		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 = -8;

		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 = 14;
		System.out.println("The absolute value of " + tempNumber1 + " is " + abs(tempNumber1));
		tempNumber1 = -2;
		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


输出结果:

The absolute value of 13 is 13
The absolute value of -8 is 8
The absolute value of 14 is 14
The absolute value of -2 is 2

java学习第四天: 闰年的计算

学习:

4.1 if 语句的嵌套.
4.2 基本规律自行百度.
4.3 布尔类型.

遇到的问题:

1.需要知道什么是布尔类型?

   通过视频学习得到布尔类型有true跟false

2.闰年的计算首先我们得知道什么是闰年?

   闰年判定方法:能被400整除或者能被4整除但不能被100整除。

代码输入:

package basic;


public class LeapYear {
	
	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 (!isLeapYear(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

	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

	
	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 isLeapYear

}// Of class LeapYear

输出结果:

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.

java学习第五天: 基本switch 语句

学习:

5.1 Switch, case, break, default 的用法.
5.2 单元测试单独使用一个方法, main 方法里面的代码越少越好.

遇到的问题:

什么是switch语句?

通过视频学习

代码输入:

package basic;


public class SwitchStatement {
	
	public static void main(String args[]) {
		scoreToLevelTest();
	}// Of main

	
	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

	
	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

输出结果:

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

java学习第六天: 基本for 语句

学习:

6.1 循环语句是程序的核心.
6.2 算法的时间复杂度一般根据循环语句来计算.

通过视频学习得到:for语句是可以用来循环的跟while循环差不多

代码输入:

package basic;

public class ForStatement {
	
	public static void main(String args[]) {
		forStatementTest();
	}// Of main

	
	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

	
	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

	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

输出结果:

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

java学习第七天: 矩阵元素相加

学习:

7.1 矩阵的赋值.
7.2 二重循环.

刚开始在b站没有搜到java矩阵元素相加的视频,通过询问得知这个属于数组模块的相关视频,通过视频学习得:

代码输入:

package basic;

import java.util.Arrays;

public class MatrixAddition {

	public static void main(String args[]) {
		matrixElementSumTest();

		matrixAdditionTest();
	}// Of main

	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


	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


	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


	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

输出结果:

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]]
The new matrix is: 
[[0, 2, 4, 6], [20, 22, 24, 26], [40, 42, 44, 46]]

java学习第八天: 矩阵相乘

学习:

8.1 三重循环是多数程序的极限.
8.2 非法输入检查是程序正常运行的基本保障. 如果检查所有的非法输入, 会导致大量代码行, 这在商业代码中是必须的.

通过查阅资料得:在java编写矩阵相乘需要实现一个三重循环,传统的矩阵乘法实现首先,两个矩阵能够相乘,必须满足一个前提:前一个矩阵的行数等于后一个矩阵的列数。第一个矩阵的第m行和第二个矩阵的第n列的乘积和即为乘积矩阵第m行第n列的值。观看闵老师的代码得知。

代码输入:

package basic;

import java.util.Arrays;

public class MatrixMultiplication {

	public static void main(String args[]) {
		matrixMultiplicationTest();
	}// Of main

	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;
			} // Of for j
		} // Of for i
		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;
			} // Of for j
		} // Of for i
		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));
	}// Of matrixMultiplicationTest


	public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
		int m = paraFirstMatrix.length;
		int n = paraFirstMatrix[0].length;
		int p = paraSecondMatrix[0].length;

		// Step 1. Dimension check.
		if (paraSecondMatrix.length != n) {
			System.out.println("The two matrices cannot be multiplied.");
			return null;
		} // Of if

		// Step 2. 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];
				} // Of for k
			} // Of for i
		} // Of for j

		return resultMatrix;
	}// Of matrixMultiplicationTest

}// Of class MatrixMultiplication

输出结果:

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]]
The new matrix is: 
[[0, 2, 4, 6], [20, 22, 24, 26], [40, 42, 44, 46]]

java学习第九天:  while 语句

学习:

9.1 while 语句本质上比 for 更基础, 因此可以替代后者. 但 for 在很多时候更方便.
9.2 break 语句又出现了, 上次是在 switch 语句里. 都是表示跳出当前代码块.

通过学习得知while语句跟前两天学习的for语句差不多都是循环语句结构如下

代码输入:

package basic;

public class WhileStatement {
	public static void main(String args[]) {
		whileStatementTest();
	}// Of main

	public static void whileStatementTest() {
		int tempMax = 100;
		int tempValue = 0;
		int tempSum = 0;

		// Approach 1.
		while (tempSum <= tempMax) {
			tempValue++;
			tempSum += tempValue;
			System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
		} // Of while
		tempSum -= tempValue;

		System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);

		// Approach 2.
		System.out.println("\r\nAlternative approach.");
		tempValue = 0;
		tempSum = 0;
		while (true) {
			tempValue++;
			tempSum += tempValue;
			System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);

			if (tempMax < tempSum) {
				break;
			} // Of if
		} // Of while
		tempSum -= tempValue;

		System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
	}// Of whileStatementTest
}// Of class WhileStatement

输出结果:

tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91

java学习第十天:  综合任务 1

学习:

学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:

进行学生成绩的随机生成, 区间为 [50, 100].
找出成绩最好、最差的同学。但有挂科的同学不参加评比.
10.1 实际代码中,for 和 if 是最常见的, switch 和 while 使用少得多.
10.2 使用了 continue, 它是指继续跳过本次循环后面的代码,直接进入下一次循环. 而 break 是跳出整个循环体.
10.3 为了随机数,迫不得已提前使用了 new 语句生成对象.
10.4 通过数据测试找出程序中的 bug.

通过学习发现生成随机数不可以直接生成50到65之间的数,得通过50+随机数(65-50)来生成

代码输入:

package basic;

import java.util.Arrays;
import java.util.Random;


public class task1 {
	
	public static void main(String args[]) {
		task1();
	}// Of main

	
	public static void task1() {
		// Step 1. Generate the data with n students and m courses.
		// Set these values by yourself.
		int n = 10;
		int m = 3;
		int lowerBound = 50;
		int upperBound = 100; // Should be 100. I use this value for testing.
		int threshold = 60;

		// Here we have to use an object to generate random numbers.
		Random tempRandom = new Random();
		int[][] data = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				data[i][j] = lowerBound + tempRandom.nextInt(upperBound - lowerBound);
			} // Of for j
		} // Of for i

		System.out.println("The data is:\r\n" + Arrays.deepToString(data));

		// Step 2. Compute the total score of each student.
		int[] totalScores = new int[n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (data[i][j] < threshold) {
					totalScores[i] = 0;
					break;
				} // Of if

				totalScores[i] += data[i][j];
			} // Of for j
		} // Of for i

		System.out.println("The total scores are:\r\n" + Arrays.toString(totalScores));

		// Step 3. Find the best and worst student.
		// Typical initialization for index: invalid value.
		int tempBestIndex = -1;
		int tempWorstIndex = -1;
		// Typical initialization for best and worst values.
		// They must be replaced by valid values.
		int tempBestScore = 0;
		int tempWorstScore = m * upperBound + 1;
		for (int i = 0; i < n; i++) {
			// Do not consider failed students.
			if (totalScores[i] == 0) {
				continue;
			} // Of if

			if (tempBestScore < totalScores[i]) {
				tempBestScore = totalScores[i];
				tempBestIndex = i;
			} // Of if

			if (tempWorstScore > totalScores[i]) {
				tempWorstScore = totalScores[i];
				tempWorstIndex = i;
			} // Of if
		} // Of for i

		// Step 4. Output the student number and score.
		if (tempBestIndex == -1) {
			System.out.println("Cannot find best student. All students have failed.");
		} else {
			System.out.println("The best student is No." + tempBestIndex + " with scores: "
					+ Arrays.toString(data[tempBestIndex]));
		} // Of if

		if (tempWorstIndex == -1) {
			System.out.println("Cannot find worst student. All students have failed.");
		} else {
			System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
					+ Arrays.toString(data[tempWorstIndex]));
		} // Of if
	}// Of task1

}// Of class Task1

输出结果:

The data is:
[[50, 51, 90], [87, 53, 72], [78, 57, 85], [56, 82, 96], [79, 65, 96], [89, 59, 77], [86, 71, 80], [79, 60, 53], [83, 88, 68], [69, 84, 59]]
The total scores are:
[0, 0, 0, 0, 240, 0, 237, 0, 239, 0]
The best student is No.4 with scores: [79, 65, 96]
The worst student is No.6 with scores: [86, 71, 80]

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值