递归调用的例子:
/**
* 99瓶啤酒
*/
public class ConditionTest16 {
public static void main(String[] args) {
game(99);
}
public static void game(int num){
System.out.println(num+"瓶啤酒在墙上,"+num+"瓶啤酒,传给他人");
num=num-1;
if (num == 0){
System.out.println("没有啤酒在墙上,没有啤酒,不能再拿了,不能在再传了,因为墙上没有啤酒了");
return;//返回语句
}
game(num);//自己调用自己
}
}
条件语句以及逻辑语句的例子:
public class Test56 {
public static void main(String[] args) {
int amount = compute(20000, 1500, true, true, true);
System.out.println(amount);
amount = compute(40000, 1500, true, true, true);
System.out.println(amount);
}
/**
* 计算个人缴纳的个税
*
* @param income 工资收入
* @param socialSecurity 5险一金金额
* @param onlyChild 是否独生子女
* @param hasChild 是否有小孩在读书
* @param hasParents 是否有60岁以上的父母需要赡养
* @return
*/
public static int compute(int income, int socialSecurity, boolean onlyChild, boolean hasChild, boolean hasParents) {
int total = income - 5000 - socialSecurity;
if (hasChild) {
total = total - 1000;
}
if (hasParents && onlyChild) {
total = total - 2000;
}
if (total >= 3000 && total < 12000) {
total = total * 10 / 100 - 210;
} else if (total >= 12000 && total < 25000) {
total = total * 20 / 100 - 1410;
} else if (total >= 25000 && total < 35000) {
total = total * 25 / 100 - 2660;
}
return total;
}
}