学习来源:https://blog.csdn.net/minfanphd/article/details/116974889
1、第一个程序,打印"Hello World!"
package 日撸java三百行day1到day10;
public class 日撸java三百行day1 {
public static void main(String[] args) {
System.out.println("Hello, World!");
}//Of main
}//Of 日撸java三百行day1
2、第二个程序,基本算数操作
2.1:加、减、乘、除、整除、取余。
2.2:熟悉 println 的中阶用法。
package 日撸java三百行day1到day10;
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
3、第三个程序,基本if语句
3.1 if then else.
3.2 方法(函数)调用: 增加代码的复用性.
3.3 方法(函数)头部规范的注释, 是后期生成文档的基础.
package 日撸java三百行day1到day10;
public class IfStatement {
public static void main(String[] args) {
int tempNumber1, tempNumber2;
//Try a positive value.尝试正值
tempNumber1 = 15;
if(tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
}else {
tempNumber2 = -tempNumber1;
}//Of if
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
//Try a negative number.尝试负值
tempNumber1 = -7;
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 = -3;
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 IfStatement
4、第四个程序,闰年的计算
4.1 if 语句的嵌套.
4.2 布尔类型.
package 日撸java三百行day1到day10;
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
5、第四个程序,基本Switch语句
5.1 Switch, case, break, default 的用法.
5.2 单元测试单独使用一个方法, main 方法里面的代码越少越好.
package 日撸java三百行day1到day10;
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 rangs 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:
resultLevel = 'C';
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 = 92;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
tempScore = 85;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
tempScore = 75;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
tempScore = 69;
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



