序言:我本科期间学习过Java程序与设计这门课程,由于时间久远,有些地方已经遗忘。现跟着闵帆老师重拾Java基础,同时学习老师的代码书写格式。
输出 Hello word
package com.day01;
/**
* This is the first code.
* @author Haitao [email protected]
*/
public class Day01 {
public static void main(String args[]) {
System.out.println("Hello, world!");
}//Of main
}//Of class Day01
运行效果
分析:
基本算数操作
package com.day01;
/**
* This is the second code.
* @author Haitao [email protected]
*/
public class Day01 {
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 Day01
运行结果
分析
若用Int加上Double(如上图),则结果会自动转化为精度较高的Double。
基本if语句
if ...else... 语句
if...else if....else 语句
案列代码
package com.day01;
/**
* The usage of the if statement.
*
* @author Haitao [email protected]
*/
public class Day01 {
/**
*********************
* 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
// Lines 27 through 33 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
/**
*********************
* 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 Day01
运行结果
闰年的计算
分析
package com.day01;
/**
* The complex usage of the if statement.
*
* @author Haitao [email protected]
*/
public class Day01 {
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
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
/**
*********************
* 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 Day01
运行结果