/** * This is the fifth code. Names and comments should follow my style strictly. * * @author Shihuai Wen wshysxcc@outlook.com. */ publicclassSwitchStatement {
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { scoreToLevelTest(); }// Of main
/** ********************* * Score to level. * * @param paraScore From 0 to 100. * @return The level from A to F. ********************* */ publicstaticcharscoreToLevel(int paraScore) { // E stands for error, and F stands for fail. charresultLevel='E';
// Divide by 10, the result ranges from 0 to 10 inttempDigitalLevel= paraScore / 10;
// The use of break is important. switch (tempDigitalLevel) { case10: case9: resultLevel = 'A'; break; case8: resultLevel = 'B'; break; case7: resultLevel = 'C'; break; case6: resultLevel = 'D'; break; case5: case4: case3: case2: case1: case0: resultLevel = 'F'; break; default: resultLevel = 'E'; }// Of switch
return resultLevel; }// of scoreToLevel
/** ********************* * Method unit test. ********************* */ publicstaticvoidscoreToLevelTest() { inttempScore=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
/** * This is the sixth code. Names and comments should follow my style strictly. * * @author ShiHuai Wen shihuaiwen@outlook.com. */ publicclassForStatement {
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { forStatementTest(); }// Of main
/** ********************* * Method unit test. ********************* */ publicstaticvoidforStatementTest() { inttempN=10; System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
tempN = 0; System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
inttempStepLength=1; tempN = 10; System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: " + addToNWithStepLength(tempN, tempStepLength));
tempStepLength = 2; System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: " + addToNWithStepLength(tempN, tempStepLength)); }// Of forStatementTest
/** ********************* * Add from 1 to N. * * @param paraN The given upper bound. * @return The sum. ********************* */ publicstaticintaddToN(int paraN) { intresultSum=0;
for (inti=1; i <= paraN; i++) { resultSum += i; } // Of for i
return resultSum; }// Of addToN
/** ********************* * Add from 1 to N with a step length. * * @param paraN The given upper bound. * @param paraStepLength The given step length. * @return The sum. ********************* */ publicstaticintaddToNWithStepLength(int paraN, int paraStepLength) { intresultSum=0;
for (inti=1; i <= paraN; i += paraStepLength) { resultSum += i; } // Of for i