第一周,博客

2021/12/24 23:38:28

本文主要是介绍第一周,博客,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

        这篇博客写得很仓促,对某些部分可能掌握不是很好请见谅。第二题代码经过转载,已进行说明。

本人使用eclipse编写java(也要用记事本写,就是效率有点低),环境早已经配置完毕,就不做截图演示了。

2021.12.22

以下是一些java基础编程题

 编写HelloWorld.java. 一定要注意变量的写法.

一:以下创建了一个HelloWorld.java文件(现在什么还没有):

package basic;

public class HelloWorld {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello,World!");
	}

}

二:基本加减乘除运算(转载)

基本的加减乘除运算,数据类型保持相同。

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
————————————————
版权声明:本文为CSDN博主「闵帆」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/minfanphd/article/details/116933803

三:if语句的使用:

package basic;

public class Operation1 {
	
	public static void main(String[] args) {
		int a,b;
		a = 10;
		if(a>=0) System.out.println("a大于等于0");
		else System.out.println("a小于0");
		
		b = -1;
		if(b>=0) System.out.println("b大于等于0");
		else System.out.println("b小于0");
		
		double c,d;
		c = 1.2;
		d = 1.9;
		if(c>d) System.out.println("c大于等于d");
		else System.out.println("c小于d");
		
		//调用绝对值函数
		int tempvalue = -16;
		System.out.println("The absolute value of "+tempvalue+" is "+numabs(tempvalue));
	}
		/**
		 * 求绝对值的函数
		 */
	public static int numabs(int num) {
		if(num>=0) return num;
		else return -num;
	}

}
a大于等于0
b小于0
c小于d
The absolute value of -16 is 16

if语句的简单运用,使用函数提高复用性,方法(函数)头部规范的注释, 是后期生成文档的基础.

还有else if,else,用法和c语言中的类似,不再举例。

四:闰年的计算:

if 语句的嵌套.
布尔类型.

闰年条件:1:年份能够被100整除,还必须被400整除。2:年份能够被4整除,但同时不能被100整除。

package basic;

public class Operation1 {

	public static void main(String[] args) {
		int year1 = 2004;
		System.out.println(isLeapYearV2(year1));
		int year2 = 2010;
		System.out.println(isLeapYearV2(year2));
		int year3 = 2012;
		System.out.println(isLeapYearV2(year3));
	}

	/**
	 * Is the given year leap? Replace the complex condition with a number of if.
	 */
	public static boolean isLeapYearV2(int paraYear) {
		if (paraYear % 100 == 0) {
			if (paraYear % 400 == 0) {
				return true;
			}
			return false;
		}

		if (paraYear % 4 == 0) {
			if (paraYear % 100 != 0) {
				return true;
			}
		}
		return false;

	}

}
true
false
true

2021.12.23日,今天继续

第 5 天: 基本switch 语句

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

能用于switch判断的类型有:byte、short、int、char(JDK1.6),还有枚举类型,但是在JDK1.7后添加了对String类型的判断

注意:switch-case可以做范围匹配,比如case score>90 但是不推荐。

package basic;

public class SwitchStatement {

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

	}
	public static char scoreToLevel(int parascore) {
		int test = parascore/10;
		char result = 'E';
		switch(test) {
		case 10:
		case 9:result = 'A';break;
		case 8:result = 'B';break;
		case 7:result = 'C';break;
		case 6:result = 'D';break;
		case 5:
		case 4:
		case 3:
		case 2:
		case 1:
		case 0:result = 'F';break;
		default:result = 'E';
		}
		return result;
	}
	/**
	 *********************
	 * Method unit test.
	 *********************
	 */
	public static void scoreToLevelTest() {
		int tempScore = 98;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		tempScore = 70;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		tempScore = 54;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		tempScore = 32;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		tempScore = 81;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
	}//of scoreToLevelTest
}//of main

Score 98 to level is: A
Score 70 to level is: C
Score 54 to level is: F
Score 32 to level is: F
Score 81 to level is: B

第 6 天: 基本for 语句

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

这里有按初始增长量1进行增长的,也有按自己设置的增长量进行增长的。

package basic;

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 = 12;
		System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
		tempN = 100;
		System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
		tempN = 45;
		System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
		System.out.println("1 add to " + tempN + " with step length " + 3 + " is: "
				+ addToNWithStepLength(tempN, 3));
	}
	
	/**
	 *********************
	 * Add from 1 to N.
	 * 
	 * @param paraN The given upper bound.
	 * @return The sum.
	 *********************
	 */
	public static int addToN(int tempN) {
		int sum = 0;
		for(int i = 1;i<=tempN;i++) {
			sum += i;
		}
		return sum;
	}
	
	/**
	 *********************
	 * 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 sum = 0;
		for(int i = 1;i<=paraN;i+=paraStepLength) {
			sum+=i;
		}
		return sum;
	}
}
1 add to 10 is: 55
1 add to 12 is: 78
1 add to 100 is: 5050
1 add to 45 is: 1035
1 add to 45 with step length 3 is: 330

第 7 天: 矩阵元素相加

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

矩阵相加,应该保证矩阵的行列相同,首先使用二重循环计算出相加后的矩阵和。在进行结果输出时,也可以使用二重循环输出,或者Arrays.deepToString输出(注意导入包)。

package basic;
import java.util.Arrays;
public class MatrixAddition {

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

	}
//	求矩阵内部和
	public static int martixsum1(int[][] martix1){
		int result = 0;
		for(int i = 0;i<martix1.length;i++) {
			for(int j = 0;j<martix1[i].length;j++) {
				result += martix1[i][j]; 
			}
		}
		return result;
	}
//	求两个矩阵之和
	public static int[][] martixsum2(int[][] martix1,int[][] martix2){
		int[][] martix3 = new int[martix1.length][martix1[0].length];
		for(int i = 0;i<martix1.length;i++) {
			for(int j = 0;j<martix1[i].length;j++) {
				martix3[i][j] += martix1[i][j]+martix2[i][j]; 
			}
		}
		return martix3;
	}
//	测试方法
	public static void martixsumTest() {
		int[][] martix1 = {{1,2,3},{4,5,6},{7,8,9}};//定义3x3的矩阵
		int[][] martix2 = {{10,11,12},{13,14,15},{16,17,18}};
		System.out.println("The matrix1 element sum is:"+martixsum1(martix1));
		System.out.println("The matrix2 element sum is:"+martixsum1(martix2));
		//接受新的矩阵
		int[][] martix3 = martixsum2(martix1,martix2);
		//输出新的矩阵
		System.out.println("The new martix3 is"+Arrays.deepToString(martix3));
		//输出新矩阵的内部的和
		System.out.println("The matrix3 element sum is:"+martixsum1(martix3));
	}
}
The matrix1 element sum is:45
The matrix2 element sum is:126
The new martix3 is[[11, 13, 15], [17, 19, 21], [23, 25, 27]]
The matrix3 element sum is:171

第 8 天: 矩阵相乘

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

这串代码是看了老师的思路才写出来的,自己三重循环一直想不出来TAT,基本是围绕矩阵的基本运算方法。

其他还有可以求出矩阵的方法,不过都要去计算矩阵,对于计算机计算意义不大。(人自己来计算的话,可以简化过程,计算机算的快,影响不大)。d

package basic;
import java.util.Arrays;
public class MatrixMultiplication {

	public static void main(String[] args) {
		martixTest();
	}
	
	public static int[][] martixMultiple(int[][] martix1,int[][] martix2){
		int[][] result = new int[martix1.length][martix2[0].length];
		int sum;
		sum = 0;
		for(int i = 0;i<martix1.length;i++) {
			for(int j = 0;j<martix2[0].length;j++) {
				for(int k = 0;k < martix1[0].length;k++) {
					result[i][j]+= martix1[i][k]*martix2[k][j];
				}
			}
		}
		return result;
	}
//	进行测试
	public static void martixTest() {
		int[][] martix1 = {{1,2,3},{3,3,2}};
		int[][] martix2 = {{1,2},{4,3},{1,4}};
		//得出结果应该是2x2的矩阵,martix1在前
		int[][] martix3 = martixMultiple(martix1,martix2);
		System.out.println("The final martix is "+Arrays.deepToString(martix3));
	}
	
}
The final martix is [[12, 20], [17, 23]]

第 9 天: while 语句(2021.12.24)

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

在循环控制时,不要使用浮点值来比较值是否相等。 因为浮点值都是某些值的近似值,使用他们可能导致不精确的循环次数和不准确的结果。

package basic;

public class WhileStatement {

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

	public static void WhileStatementTest() {
		int tempMax = 100;
		int tempValue = 0;
		int tempSum = 0;
		// 法一
		while (tempMax > tempValue) {
			tempValue += 1;
			tempMax -= tempValue;
			if (tempMax < tempValue)
				break;
			System.out.println("tempMax:" + tempMax + ">" + "tempValue:" + tempValue);
		}
		tempMax = 100;
		tempValue = 0;
		tempSum = 0;
		// 法二:
		while (tempMax > tempSum) {
			tempValue += 1;
			tempSum += tempValue;
			tempMax -= 3;
			if (tempMax < tempSum)
				break;
			System.out.println("tempMax:" + tempMax + ">" + "tempSum:" + tempSum);
		}
	}
}
tempMax:99>tempValue:1
tempMax:97>tempValue:2
tempMax:94>tempValue:3
tempMax:90>tempValue:4
tempMax:85>tempValue:5
tempMax:79>tempValue:6
tempMax:72>tempValue:7
tempMax:64>tempValue:8
tempMax:55>tempValue:9
tempMax:45>tempValue:10
tempMax:34>tempValue:11
tempMax:22>tempValue:12
tempMax:97>tempSum:1
tempMax:94>tempSum:3
tempMax:91>tempSum:6
tempMax:88>tempSum:10
tempMax:85>tempSum:15
tempMax:82>tempSum:21
tempMax:79>tempSum:28
tempMax:76>tempSum:36
tempMax:73>tempSum:45
tempMax:70>tempSum:55
tempMax:67>tempSum:66

第 10天: 综合任务 1

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

  1. 进行学生成绩的随机生成, 区间为 [50, 100].
  2. 找出成绩最好、最差的同学。但有挂科的同学不参加评比.

              

package basic;

import java.util.Random;
import java.util.Arrays;
public class Task1 {

	public static void main(String[] args) {
		Task1Test();
	}
	
	public static void Task1Test() {
		Random rand = new Random();//随机数种子
		int[][] result = new int[10][3];
		for(int i = 0;i < 10;i++) {
			for(int j = 0;j < 3;j++) {
				result[i][j] = (int)(rand.nextDouble()*50+50);
			}
		}
		System.out.println("当前所有同学如下:");
		System.out.println(Arrays.deepToString(result));
		
		int maxscore = 0,newscore;//最好成绩与当前成绩
		int flag1 = -1;//标记最好同学的位置
		int flag2;//标记不及格的同学
		for(int i = 0;i <10;i++) {
			flag2 = 0;
			newscore = 0;
			for(int j = 0;j<3;j++) {
				if(result[i][j]<60) {
					flag2 = 1;
					break;
				}
			}
			if(flag2==1) continue;
			for(int k = 0;k<3;k++) {
				newscore += result[i][k];
			}
			if(newscore>maxscore) {
				flag1 = i;
				maxscore = newscore;
			}
		}
		flag1+=1;
		System.out.println("成绩最好的学生是:"+flag1+"号学生,他的语文是"+result[flag1-1][0]+"分,数学是"+result[flag1-1][1]+"分,英语是"+result[flag1-1][2]+"分");
	}
	
	
}
当前所有同学如下:
[[76, 62, 92], [51, 87, 99], [76, 99, 84], [65, 69, 57], [58, 93, 77], [59, 75, 77], [65, 79, 78], [62, 63, 53], [89, 92, 80], [91, 53, 94]]
成绩最好的学生是:9号学生,他的语文是89分,数学是92分,英语是80分

这道题总体思路就是先给所有同学随机赋成绩,使用二重循环得到最高成绩,在循环过程中筛掉成绩不及格的同学,我采用了flag进行标识。总体思路比较简单。不知道有没有更简单的办法。

以下补充上最近的背单词情况:

 



这篇关于第一周,博客的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程