基本if语句
if语句用于设置条件,选择性的执行某段代码。
if分支结构使用说明:
- 条件表达式必须是布尔表达式(关系表达式或逻辑表达式)、布尔变量
- 语句块只有一条执行语句时,一对{}可以省略,但建议保留
- if-else语句结构,根据需要可以嵌套使用
- 当多个条件是“互斥”关系时,条件判断语句及执行语句间顺序无所谓;当多个条件是“包含”关系时,“小上大下 / 子上父下”
代码中还用到了函数(方法),提高了代码的复用性。
函数(方法)的头部规范注释,用于说明该函数的作用、参数的意义等,为生成JavaDoc做准备。
package day03;
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.
// Lines 25 through 33 are same as Lines 13 through 21.
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 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));
}
/**
* 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
运行效果:
应用:闰年的计算
涉及到if的嵌套应用,使用布尔类型。
如何判断某年是否是闰年:
- 年份能被4整除并且不能被100整除是闰年,如:100年,不是闰年;2008年,是闰年;
- 年份能被400整除是闰年,如:2000年,是闰年;
输出语句:
- System.out.println() :输出完内容后换行。
- System.out.print():输出完内容后不换行。
package day03;
public class LeapYear {
/**
* 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.
* @return A boolean value.
*/
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.
* @return A boolean value.
*/
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
运行效果:
浅试生成JavaDoc,感受头部注释的作用:
-encoding UTF-8 -charset UTF-8
switch基本语句
switch-case的使用:
switch(表达式){
case 常量1:
语句1;
// break;
case 常量2:
语句2;
// break;
……
case 常量N:
语句N;
// break;
default:
语句;
// break;
}
switch语句有关规则:
- switch(表达式)中表达式的值必须是下述几种类型之一:byte,short,char,int,枚举 (jdk 5.0),String (jdk 7.0);
- case子句中的值必须是常量,不能是变量名或不确定的表达式值;
- 同一个switch语句,所有case子句中的常量值互不相同;
- break语句用来在执行完一个case分支后使程序跳出switch语句块;如果没有break,程序会顺序执行到switch结尾;
- 当没有匹配的case时,执行default。
package day03;
public class SwitchStatement {
/**
* The entrance of the 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
/**
* Method unit test.
*/
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
运行效果: