4.2 Advanced Control Structures
return
- 利用 return 會退出方法的特性,可以省略某些 else
public class IfElse2 {
static int test(int testval, int target) {
if(testval > target)
return +1;
else if(testval < target)
return -1;
return 0; // Match
}
public static void main(String[] args) {
System.out.println(test(10, 5));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
} /* Output:
1
-1
0
*/