基本if语句:
输入:无输入。
输出:输出tempNumber1的绝对值。
优化目标:可能没有优化目标。
package com.java.demo;
public class IfStatement {
public static void main(String[] args) {
int tempNumber1, tempNumber2;
tempNumber1 = 10;
if(tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
}else {
tempNumber2 = -tempNumber1;
}//of if
System.out.println("The absolute value of "+tempNumber1+" is "+tempNumber2);
}//of main
}//of class IfStatement
方法(函数)调用: 增加代码的复用性。
输入:无输入。
输出:输出tempNumber1的绝对值。
优化目标:可能没有优化目标。
package com.java.demo;
public class IfStatement {
public static void main(String[] args) {
int tempNumber1, tempNumber2;
tempNumber1=10;
if(tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
}else {
tempNumber2 = -tempNumber1;
}//of if
System.out.println("The absolute value of "+tempNumber1+" is "+tempNumber2);
tempNumber1 = 6;
System.out.println("The absolute value of "+tempNumber1+" is "+abs(tempNumber1));
tempNumber1 = -7;
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
闰年的计算
输入:无输入。
输出:若tempYear为闰年则输出:tempYear is a leap year,若非闰年则输出:tempYear is NOT a leap year。
优化目标:可能没有优化目标。
package com.java.demo;
public class LeapYear {
public static void main(String[] args) {
int tempYear = 2022;
System.out.print(" " + tempYear + " is ");
if(!isLeapYear(tempYear)) {
System.out.print("NOT");
}//of if
System.out.print(" a leap year.");
tempYear = 2020;
System.out.print(" " + tempYear + " is ");
if(!isLeapYear(tempYear)) {
System.out.print("NOT");
}//of if
System.out.print(" a leap year.");
tempYear = 2000;
System.out.print(" "+tempYear+" is ");
if(!isLeapYear(tempYear)) {
System.out.print("NOT");
}//of if
System.out.print(" a leap year.");
tempYear = 2004;
System.out.print(" "+tempYear+" is ");
if(!isLeapYear(tempYear)) {
System.out.print("NOT");
}//of if
System.out.print(" a leap year.");
//test isLeapYearV2
System.out.println("Now use the second version.");
tempYear = 2022;
System.out.print(" "+tempYear+" is ");
if(!isLeapYearV2(tempYear)) {
System.out.print("NOT");
}//of if
System.out.print(" a leap year.");
tempYear = 2020;
System.out.print(" "+tempYear+" is ");
if(!isLeapYearV2(tempYear)) {
System.out.print("NOT");
}//of if
System.out.print(" 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
在使用System.out.println语句时,在输出完要输出的语句后会自动换行,而使用System.out.print语句时不会换行,会从上一句输出的位置后继续输出。
Switch, case, break, default 的用法。
输入:无输入。
输出:
Score 100 to level is: A
Score 91 to level is: A
Score 85 to level is: B
Score 75 to level is: C
Score 65 to level is: D
Score 50 to level is: F
Score 5 to level is: F
Score 120 to level is: E
优化目标:可能没有优化目标。
package com.java.demo;
public class SwitchStatement {
public static void main(String[] args) {
scoreToLevelTest();
}//of main
public static char scoreToLevel(int paraScore) {
char resultLevel;
int tempDigitalLevel = paraScore/10;
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 = 85;
System.out.println("Score "+tempScore+" to level is: "+ scoreToLevel(tempScore));
tempScore = 75;
System.out.println("Score "+tempScore+" to level is: "+ scoreToLevel(tempScore));
tempScore = 65;
System.out.println("Score "+tempScore+" to level is: "+ scoreToLevel(tempScore));
tempScore = 50;
System.out.println("Score "+tempScore+" to level is: "+ scoreToLevel(tempScore));
tempScore = 5;
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