Java语言入门逻辑语句、方法 - 图1递归调用的例子:

    1. /**
    2. * 99瓶啤酒
    3. */
    4. public class ConditionTest16 {
    5. public static void main(String[] args) {
    6. game(99);
    7. }
    8. public static void game(int num){
    9. System.out.println(num+"瓶啤酒在墙上,"+num+"瓶啤酒,传给他人");
    10. num=num-1;
    11. if (num == 0){
    12. System.out.println("没有啤酒在墙上,没有啤酒,不能再拿了,不能在再传了,因为墙上没有啤酒了");
    13. return;//返回语句
    14. }
    15. game(num);//自己调用自己
    16. }
    17. }

    条件语句以及逻辑语句的例子:

    1. public class Test56 {
    2. public static void main(String[] args) {
    3. int amount = compute(20000, 1500, true, true, true);
    4. System.out.println(amount);
    5. amount = compute(40000, 1500, true, true, true);
    6. System.out.println(amount);
    7. }
    8. /**
    9. * 计算个人缴纳的个税
    10. *
    11. * @param income 工资收入
    12. * @param socialSecurity 5险一金金额
    13. * @param onlyChild 是否独生子女
    14. * @param hasChild 是否有小孩在读书
    15. * @param hasParents 是否有60岁以上的父母需要赡养
    16. * @return
    17. */
    18. public static int compute(int income, int socialSecurity, boolean onlyChild, boolean hasChild, boolean hasParents) {
    19. int total = income - 5000 - socialSecurity;
    20. if (hasChild) {
    21. total = total - 1000;
    22. }
    23. if (hasParents && onlyChild) {
    24. total = total - 2000;
    25. }
    26. if (total >= 3000 && total < 12000) {
    27. total = total * 10 / 100 - 210;
    28. } else if (total >= 12000 && total < 25000) {
    29. total = total * 20 / 100 - 1410;
    30. } else if (total >= 25000 && total < 35000) {
    31. total = total * 25 / 100 - 2660;
    32. }
    33. return total;
    34. }
    35. }