栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

跟着老师学Java Day5-Day10

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

跟着老师学Java Day5-Day10

Day5:基本Switch语句

5.1 Switch,case,break,default 的用法

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

5.1代码
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
5.2运行结果

Day6: 基本for 语句

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

6.1代码
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
6.2运行结果

 Day7: 矩阵元素相加

7.1 矩阵的赋值

7.2  二重循环

7.1代码
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: rn" + Arrays.deepToString(tempMatrix));
		System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "rn");
	}//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;
	}
	
	
	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: rn" + Arrays.deepToString(tempMatrix));
		int[][] tempNewMatrix = matrixAddition(tempMatrix,tempMatrix);
		System.out.println("The new matrix is: rn" + Arrays.deepToString(tempNewMatrix));
	}//Of matrixAdditionTest
}// Of class MatrixAddition
7.2 运行结果

7.3 不太会的。。

 1、通过new申请空间,不释放:int[][] tempMatrix = new int[3][4];

2、矩阵相关操作    import java.util.Arrays;   Arrays.deepToString(tempMatrix)

Day8: 矩阵相乘

8.1 三重循环是多数程序的极限

8.2非法输入检查是程序正常运行的基本保障.如果检查所有的非法输入,会导致大量代码行,这在商业代码中是必须的。

8.1代码
package basic;

import java.util.Arrays;



public class MatrixMultiplication {
	
	
	public static void main(String args[]) {
		matrixMultiplicationTest();
	}// Of main
	
	
	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 matrix cannot be multiplied.");
			return null;
		}//Of if
		
		//Step2. The loop.
		int[][] resultMatrix = new int[m][p];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < p; j++) {
				resultMatrix[i][j] = 0;
				for (int k = 0; k < n; k++) {
					resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
				}// Of for k
			}// Of for j
		}// Of for i
		
		return resultMatrix;
	}// Of multiplication
	
	
	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: rn" + 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: rn" + Arrays.deepToString(tempSecondMatrix));
		
		int[][] tempThirdMatrix = new int[2][2];
		System.out.println("The third matrix is: rn" + Arrays.deepToString(multiplication(tempFirstMatrix, tempSecondMatrix)));
	    
		System.out.println("Trying to multiply the first matrix with itself.rn");
		tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
		System.out.println("The result matrix is: rn" + Arrays.deepToString(tempThirdMatrix));
	}// Of matrixMultiplicationTest
	
}// Of class MatrixMultiplication
8.2运行结果

 Day9:while语句

while几乎可以替代for,但for用起来简洁方便。

9.1代码
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("rnAlternative approach.");
		tempValue = 0;
		tempSum = 0;
		while (true) {
			tempValue++;
			tempSum += tempValue;
			System.out.println("temValue = " + 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
9.2运行结果

9.3心得

while(条件)= while(真){if(!条件),break;} 

Day10: 综合任务1

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

1、进行学生成绩的随机生成,区间为[50,100].

2、找出成绩最好、最差的同学。但有挂科的同学不参与评比。

10.1代码
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() {
		// Step1. Generate the data with n students and m courses.
		int n = 10;
		int m = 3;
		int lowerBound = 50;
		int upperBound = 100;
		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:rn" + Arrays.deepToString(data));

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

		System.out.println("The totalScores is:rn" + 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.
			//Make a flag bit and jump out of the two-layer for loop.
			int flag = 0;
			for (int j = 0; j < m; j++) {
				if (data[i][j] < threshold) {
					flag = 1;
					break;
				} // Of if
			} // Of for j
			
			//When the value of flag is 1, the student is considered unqualified.
			if ( flag == 1) {
				continue;
			}
			
			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.
		// when a student is both the best and the worst, all students have the same total scores.
		if (tempBestIndex == tempWorstIndex && tempWorstIndex != -1) {
			System.out.println("All students have the same total scores.");
		} else {			
			if (tempBestIndex == -1) {
				System.out.println("Cannot find best student. All students have faied.");
			} 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 faied.");
			} else {
				System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
						+ Arrays.toString(data[tempWorstIndex]));
			} // Of if
		}// Of if
		
	}// Of task1

}// Of class Task1
10.2 运行结果

10.3 收获

通过设置flag,可以跳出两层循环。

在 Eclipse 中使用鼠标右键 source -> format, 自动调整程序的格式。

for (int i = 0; i < n; i++) {
	// Do not consider failed students.
	//Make a flag bit and jump out of the two-layer for loop.
	int flag = 0;
	for (int j = 0; j < m; j++) {
		if (data[i][j] < threshold) {
			flag = 1;
			break;
		} // Of if
	} // Of for j
			
	//When the value of flag is 1, the student is considered unqualified.
	if ( flag == 1) {
		continue;
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978691.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号