注意:这是自己java的学习过程的记录,如有问题欢迎指正。
文章目录
目录
一、Day1: 环境搭建与eclipse的安装
二、Day2:基本算数运算
三、Day3:基本if语句的使用
四、Day4:闰年的计算(对if的嵌套使用)
五、Day5:基本switch语句
六、Day6:基本for语句的使用
七、Day7:矩阵元素的相加(二维数组与for循环的嵌套)
总结
前言
此博客的目的是为了让自己培养成更好的代码规范和具备基本的Java程序设计能力,也为了更好的为后面的机器学习打好坚实的基础。
一、Day1: 环境搭建与eclipse的安装
环境搭建与eclipse的安装可以按照B站up主的视频操作说明(环境搭建与Eclipse安装教程)很快就可以完成。
在完成环境搭建后开始我们在eclipse上的第一个程序
package basic;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}//of main
}// of class HelloWorld
运行后得到的结果是:
Java的学习之路正式开启!
二、Day2:基本算数运算
代码如下:
package basic;
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;
// Adition
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);
// Modules
tempResultInt = tempFirstInt % tempSecondInt;
System.out.println("" + tempFirstInt + " % " + tempSecondInt + " = " + tempResultInt);
}// of main
}// of class BasicOperations
关于Java变量的命名:
java的命名中,一般将方法和变量的标识符按照小驼峰式命名法命名,也就是变量的第一个单词首字母小写,而之后的单词首字母大写表示,如代码中的变量“tempFirstInt”;而类名的标识符一般用大驼峰式书写,即单词都以大写开头,如类名“BasicOperations”。
变量的命名也要做到见名知意。
提醒:Eclipse 中的 source -> format 可以快捷对代码格式进行格式化。
三、Day3:基本if语句的使用
代码如下(示例):
package basic;
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;
} // Of if
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
// Try a negative value
// Lines 27 through 31 are the same as Lines 15 through 19
tempNumber1 = -3;
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 = 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));
}// Of main
public static int abs(int paraValue) {
if (paraValue >= 0) {
return paraValue;
} else {
return -paraValue;
} // Of if
}// Of abs
}// Of class IfStatement
运行结果:
if语句的使用:
abs方法是static修饰的,Java 中被static修饰的成员称为静态成员或类成员。它属于整个类所有,而不是某个对象所有,即被类的所有对象所共享、且优先于对象存在。静态成员可以使用类名直接访问,也可以使用对象名进行访问。使用 static 可以修饰变量、方法和代码块。
静态方法中可以直接调用同类中的静态成员,但不能直接调用非静态成员。
关于static的深度学习
四、Day4:闰年的计算(对if的嵌套使用)
代码如下:
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 (!isLeapYearV2(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 isLeapYearV2
}// Of class LeapYear
运行结果:
闰年的计算是:年份为整百年时,能被四百整除的年份是闰年,为非整百年,能被四整除就是闰年
注意 if if和if else if的区别:
- 从字面上理解if为如果,就是如果这种情况,如果那种情况。else if 不是上一个条件的前提下,如果是这个条件。区别1:if无论是否满足条件都会向下执行,知道程序结束,else if 满足一个条件就会停止执行。区别2:由于if都会执行一遍,则可能会同一个需要判断的事件,会进入2个if语句中,出现错误,而else if就不会发生这样的事情
五、Day5:基本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
运行的结果:
switch语句:
当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。
六、Day6:基本for语句的使用代码如下:
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
运行的结果:
for语句:
for循环小括号里第一个";"号前为一个为不参与循环的单次表达式,其可作为某一变量的初始化赋值语句, 用来给循环控制变量赋初值; 也可用来计算其它与for循环无关但先于循环部分处理的一个表达式。
俩";"号之间的条件表达式是一个关系表达式, 其为循环的正式开端,当条件表达式成立时执行中间循环体。
执行的中间循环体可以为一个语句,也可以为多个语句,当中间循环体只有一个语句时,其大括号{}可以省略,执行完中间循环体后接着执行末尾循环体 。
执行末尾循环体后将再次进行条件判断,若条件还成立,则继续重复上述循环,当条件不成立时则跳出当下for循环。
补充增强for循环:
增强for循环只能用在数组、或实现Iterator接口的集合类上
语法格式:
for(变量类型变量 :需迭代的数组或集合){undefined
}
//增强for循环需要注意的问题:只适合取数据
七、Day7:矩阵元素的相加(二维数组与for循环的嵌套)
代码如下:
package basic;
import java.util.Arrays;
import java.util.PrimitiveIterator.OfDouble;
public class MatrixAddition {
public static void main(String[] args) {
matrixElementSumTest();
matrixAdditionTest();
}
static int matrixElementSum(int[][] matrix) {
int resultSum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
resultSum += matrix[i][j];
}
}
return resultSum;
}// of matrixElementSum
public static int[][] matrixElement_Init() {
int[][] matrixTest = new int[3][4];
for (int i = 0; i < matrixTest.length; i++) {
for (int j = 0; j < matrixTest[0].length; j++) {
matrixTest[i][j] = i * 10 + j;
} // of for j
} // of for i
return matrixTest;
}// of matrixElement_Init
public static void matrixElementSumTest() {
System.out.println("The matrix is:rn" + Arrays.deepToString(matrixElement_Init()));
System.out.println("The matrix element sum is: " + matrixElementSum(matrixElement_Init()) + "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;
}// Of matrixAddition
public static void matrixAdditionTest() {
int[][] matrix1 = matrixElement_Init();
int[][] matrix2 = matrixElement_Init();
System.out.println("The matrix1 is: rn" + Arrays.deepToString(matrix1));
System.out.println("The matrix2 is: rn" + Arrays.deepToString(matrix2));
int[][] resultMatrix = matrixAddition(matrix1, matrix2);
System.out.println("The new Matrix is: rn" + Arrays.deepToString(resultMatrix));
}// of matrixAdditionTest
}// of class MatrixAdditon
运行结果:
矩阵一般是与二维数组(双重for嵌套)相关联,矩阵也是一种非常重要的数据结构,其包含的算法却非常丰富,于基础的二维数组数据模拟复杂的矩阵算法能极大锻炼我们的代码思维与对矩阵之中包含的数学逻辑的理解。
总结
通过第一周的学习使得自己对Java又进行了复习,自己的Java代码比之前有了更加规范的格式,也在写代码过程中之前一些没有掌握到的知识进行了补充,例如if if语句和 if else if在代码的运行过程中的区别。通过对老师代码的学习也掌握到了Java的文档注释。



