来源:日撸 Java 三百行(01-10天,基本语法)_闵帆的博客-CSDN博客
1 输出Hello World
package 日撸Java300行;
/**
* This is the first code.
*@time 2022/03/31
*@author Hui Xiao
*/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}// Of main
}// Of class HelloWorld
截图:
2 基本算术操作
2.1 加、减、乘、除、整除、取余。
2.2 熟悉println的中阶用法(字符串的加法)。
package 日撸Java300行;
/**
* This is the second code.
*@time 2022/3/31
*@author Hui Xiao
*/
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;
// Addiction
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);
// Mutiplication
tempResultInt = tempFirstInt * tempSecondInt;
tempResultDouble = tempFirstDouble * tempSecondDouble;
System.out.println("" + tempFirstInt + " * " + tempSecondInt + " = " +tempResultInt);
System.out.println("" + tempFirstDouble + " * " + tempSecondDouble + " = " +tempResultDouble);
// Diviaion
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
截图:
3 基本if语句
3.1 if then else
3.2 方法(函数)调用:增加代码的复用性。
3.3 方法(函数)头部规范的注释,是后期生成文档的基础。
package 日撸Java300行;
/**
*The usage of if statement.
*@time 2022/3/31
*@author Hui Xiao
*/
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 = 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
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
/**
****************
* 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
截图:
4 闰年的计算
4.1 if语句的嵌套。
4.2 闰年:年份能被4或者400整除但不能被100整除。
4.3 布尔类型。
package 日撸Java300行;
/**
* This is the fourth code.
* The complex usage of if statement.
*@time 2022/3/31
*@author Hui Xiao
*/
public class LeapYear {
/**
**************
* The entrance of program.
*
*@param args Not used now.
**************
*/
public static void main(String[] args) {
//Test isLeapYear
int tempYear = 2021;
System.out.println("" + tempYear + " is ");
if(!isLeapYear(tempYear)) {
System.out.println("NOT ");
}// Of if
System.out.println("a leap year.");
tempYear = 2000;
System.out.println("" + tempYear + " is ");
if(!isLeapYear(tempYear)) {
System.out.println("NOT ");
}// Of if
System.out.println("a leap year.");
tempYear = 2100;
System.out.println("" + tempYear + " is ");
if(!isLeapYear(tempYear)) {
System.out.println("NOT ");
}// Of if
System.out.println("a leap year.");
tempYear = 2004;
System.out.println("" + tempYear + " is ");
if(!isLeapYear(tempYear)) {
System.out.println("NOT ");
}//Of if
System.out.println("a leap year.");
// Test isLeapYearV2
System.out.println("Now use the second version.");
tempYear = 2021;
System.out.println("" + tempYear + " is ");
if(!isLeapYearV2(tempYear)) {
System.out.println("NOT ");
}// Of if
System.out.println("a leap year.");
tempYear = 2000;
System.out.println("" + tempYear + " is ");
if(!isLeapYearV2(tempYear)) {
System.out.println("NOT ");
}// Of if
System.out.println("a leap year.");
tempYear = 2100;
System.out.println("" + tempYear + " is ");
if(!isLeapYearV2(tempYear)) {
System.out.println("NOT ");
}//Of if
System.out.println("a leap year.");
tempYear = 2004;
System.out.println("" + tempYear + " is ");
if(!isLeapYearV2(tempYear)) {
System.out.println("NOT ");
}// Of if
System.out.println("a leap year.");
}// Of main
/**************
* Is the given year leap?
*
* @param paraYear The given year.
***************
*/
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
/**
***************
* Is the given year leap? Replace the complex condition with a number of if.
*
* @param paraYear The given year.
***************
*/
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
截图:
5 基本switch语句
5.1 Switch,case,break,default的用法。
5.2 单元测试单独使用一个方法,main方法里面的代码越少越好。
package 日撸Java300行;
/**
* This is the fifth code.
*@time 2022/3/31
*@author Hui Xiao
*/
public class SwitchStatement {
/**
**************
*The entrance of program.
*
*@param args Not used now.
**************
*/
public static void main(String[] args) {
scoreToLevelTest();
}// Of main
/**
************
* Score to level
* @param paraScore From 0 to 100.
* @return The level from A to F.
************
*/
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
截图:
今日总结:之前是学过Java的,但是已经有点时间没碰过Java了,这次也顺便好好再复习一遍,并且跟着老师学习格式,让自己写代码的习惯练好。这次最主要的是将eclipse安装好,并且跟着做了五个练习,找到了一点点以前学Java的感觉,还得多练。