2021/12/8
1.完成了Eclipse安装,学习了package,import和println语句
2.理解了package要与所创建的包名一致
3.完成了helloworld
package basic;
class Helloworld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
2021/12/9
1.完成了加 减 乘 除 取余
2.熟悉了println的中阶用法
package basic;
public class BasicOprations {
public static void main(String args[])
{
int tempFirstInt,tempSecondInt,tempResultInt;
double tempFirstDouble,tempSecondDouble,tempResultDouble;
tempFirstInt = 15;
tempSecondInt = 4;
tempFirstDouble = 1.2;
tempSecondDouble=3.5;
tempResultInt=tempFirstInt + tempSecondInt;
tempResultDouble=tempFirstDouble + tempSecondDouble;
System.out.println(""+tempFirstInt+"+"+tempSecondInt+"="+tempResultInt);
System.out.println(""+tempFirstDouble+"+"+tempSecondDouble+"="+tempResultDouble);
tempResultInt=tempFirstInt - tempSecondInt;
tempResultDouble=tempFirstDouble - tempSecondDouble;
System.out.println(""+tempFirstInt+"-"+tempSecondInt+"="+tempResultInt);
System.out.println(""+tempFirstDouble+"-"+tempSecondDouble+"="+tempResultDouble);
tempResultInt=tempFirstInt*tempSecondInt;
tempResultDouble=tempFirstDouble*tempSecondDouble;
System.out.println(""+tempFirstInt+"*"+tempSecondInt+"="+tempResultInt);
System.out.println(""+tempFirstDouble+"*"+tempSecondDouble+"="+tempResultDouble);
tempResultInt=tempFirstInt/tempSecondInt;
tempResultDouble=tempFirstDouble/tempSecondDouble;
System.out.println(""+tempFirstInt+"/"+tempSecondInt+"="+tempResultInt);
System.out.println(""+tempFirstDouble+"/"+tempSecondDouble+"="+tempResultDouble);
tempResultInt=tempFirstInt%tempSecondInt;
System.out.println(""+tempFirstInt+"%"+tempSecondInt+"="+tempResultInt);
}
}
12.10
基本if语句
1.if then else
2.方法调用:增加代码的复用性.
3.方法头部规范的注释, 是后期生成文档的基础.
package basic;
public class IfStatement {
public static void main(String args[]) {
int tempNumber1, tempNumber2;
tempNumber1 = 5;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
}
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
tempNumber1 = -3;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
}
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
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));
}
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
12.11
1.if语句的嵌套
2.if语句的基本规律
if语句的格式分为三种:单分支、双分支和多分支
//基本格式
if(条件表达式){
当条件为true时执行的代码;
}else{
当条件为false时执行的代码;
}
//单if形式
if(条件表达式){
当条件为true时执行的代码;
}
//if-else-if形式
if(条件1){
}else if(条件2){
}else if(条件3){
}else{
}
//嵌套if-else形式
if(条件1){
条件1为true时执行的代码;
if(条件2){
条件2为true时执行的代码;
}else{
条件2为false时执行的代码;
}
}else{
条件1为false时执行的代码;
}
3.Boolean类型
package basic;
public class LeapYear {
public static void main(String args[]) {
//data1
int tempYear = 2021;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
}
System.out.println("a leap year.");
//data2
tempYear = 2000;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
}
System.out.println("a leap year.");
//data3
tempYear = 2100;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
}
System.out.println("a leap year.");
//data4
tempYear = 2004;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
}
System.out.println("a leap year.");
// V2
System.out.println("Now use the second version.");
//data5
tempYear = 2021;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("NOT ");
}
System.out.println("a leap year.");
//data6
tempYear = 2000;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("NOT ");
}
System.out.println("a leap year.");
//data7
tempYear = 2100;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("NOT ");
}
System.out.println("a leap year.");
//data8
tempYear = 2004;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("NOT ");
}
System.out.println("a leap year.");
}
public static boolean isLeapYear(int paraYear) {
if ((paraYear % 4 == 0) && (paraYear % 100 != 0) || (paraYear % 400 == 0)) {
return true;
} else {
return false;
}
}
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.
12.12(第五天)
基本switch语句
switch,case,break,default的用法
switch case语句
switch (整型表达式)
{
case 整型常量表达式1: 语句1
case 整型常量表达式2: 语句2
、
、
、
case 整型常量表达式n: 语句n
default: 语句n+1
}
switch()语句括号内必须是整型常量表达式,浮点类型、结构体、指针都不行。
swtich()整型常量表达式匹配哪个case整型常量表达式则直接跳转执行哪个case语句。
枚举类型也可以和switch case语句相结合使用。
break语句
在switch case语句中,我们没办法直接实现分支,搭配break使用才能实现真正的分支。
不用break则所有的case语句从匹配执行的那一个开始顺序往下全部执行
break语句的实际效果是把语句列表划分为不同的部分。break语句直接从case语句后跳出所有的switch分支语句。
default:子句
default子句
如果switch语句表达所有的值与所有的case语句的值都不匹配怎么办?
其实也没什么,结果就是所有的语句都被跳过而已,程序并不会终止,也不会报错,因为这种情况在Java中并不认为是错误的。
但是你要是并不想忽略不匹配case语句表达式的值就需要在语句列表中增加一条default子句。
写在任何一个case标签可以出现的位置。
当switch表达式的值并不匹配所有的case标签的值时,这个default子句后面的语句就会执行。
每个switch语句中只能出现一条default子句。但是他可以出现在语句列表的任何位置,而且语句留会像贯穿一个case语句一样贯穿default子句。
最好在每个swtich语句中都放一条default。
package basic;
public class SwitchStatement {
public static void main(String args[]) {
scoreToLevelTest();
}
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));
}
}
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
12.13(第六天)
基本for语句
1 循环语句是程序的核心.
2 算法的时间复杂度一般根据循环语句来计算.
package basic;
public class ForStatement {
// 函数入口
public static void main(String args[]) {
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循环的基本用法
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;
}
}
/*for循环的其他用法
* 传统方法遍历
//建立一个Collection对象
String[] strings ={"A","B","C","D"};
Collection stringList=java.util.Arrays.asList(strings);
//开始遍历
for(iterator itr=stringList.iterator();itr.hasNext();){
Object str = itr.next();
System.out.println(str);
这一种也是本人最喜欢的一种
for(循环变量类型 循环变量名称:要被遍历的对象){
循环体;
}
//建立一个数组
int[] integers={1,2,3,4};
//开始遍历
for(int i:integers){
System.out.println(i);
}
*/
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
12.14(第七天)
矩阵元素相加
1.矩阵赋值(根据矩阵的特性)
2.充分利用二重循环完成矩阵的相应操作
package basic;
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;
}
}
// Arrays里的深度遍历方法,再用System打印出数组
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
// 利用matrixElementSum()里成员直接调用该静态方法
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];
}
}
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;
}
}
// Arrays里的深度遍历方法,再用System打印出数组
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
// 定义一个新数组,该数组调用了方法matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2)
int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
// Arrays里的深度遍历方法,再用System打印出数组
System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
}
}
运行结果
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,所以这一周的撸代码对我来说还是比较轻松的,但是巩固了Java的基本语法,学习了矩阵的赋值以及相加,增强for循环,还有Arrays包里的深度优先遍历。over,下周再见。