9初级 - Java的控制流-练习

182 阅读3分钟

9-1

  • 9-1-1
package com.github.hcsp.controlflow;

public class Main {
    /**
     * 判断一个给定的年份数字是不是闰年。一个年份是闰年的条件是: 如果该年份能被100整除,那当且仅当它被400整除时,它才是闰年; 否则,当它能被4整除,就是闰年。
     *
     * <p>例如,1999年不是闰年;2000年是闰年;1900年不是闰年。
     *
     * @param year 给定的年份数字
     * @return 如果该年份是闰年,返回true,否则返回false
     */
    public static boolean isLeapYear(int year) {
        if (year % 4 == 0) {
            if ((year % 100) != 0) {
                return true;
            } else if ((year % 100 == 0) && (year % 400 == 0)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    public static void main(String[] args) {
        System.out.println(isLeapYear(1999));
        System.out.println(isLeapYear(2000));
        System.out.println(isLeapYear(1900));
        System.out.println(isLeapYear(2004));
    }
}
  • 9-1-2
package com.github.hcsp.controlflow;

public class Main {
    /**
     * 打印从start到end区间所有的奇数,包括start和end本身(若符合条件)。 注意,数字之间用英文逗号分隔。
     *
     * <p>例如,start=1,end=5,则打印1,3,5 又如,start=-2,end=2,则打印-1,1
     *
     * @param start 区间开始
     * @param end   区间结束
     */
    public static void printOddNumbersBetween(int start, int end) {
        String s = "";
        for (int i = start; i <= end; i++) {
            if (i % 2 != 0) {
                s += i + ",";
            }
            System.out.println(s);
        }
    }

    public static void main(String[] args) {
        printOddNumbersBetween(1, 5);
        printOddNumbersBetween(-2, 2);
    }
}
  • 3
package com.github.hcsp.controlflow;

import java.util.regex.Pattern;

public class Main {
    /**
     * 统计一个给定的字符串中,大写英文字母(A,B,C,...,Z)出现的次数。
     *
     * <p>例如,给定字符串"AaBbCc1234ABC",返回6,因为该字符串中出现了6次大写英文字母ABCABC
     *
     * @param str 给定的字符串
     * @return 字符串中大写英文字母出现的次数
     */
    public static int countUpperCaseLetters(String str) {
        int j = 0;
        for (int i = 0; i < str.length(); i++) {
            String pattern = "[A-Z]";
            boolean isMatch = Pattern.matches(pattern, str.charAt(i)+"");
            System.out.println(isMatch);
            if(isMatch) {
                j += 1;
            }
        }
        return j;
    }

    public static void main(String[] args) {
        countUpperCaseLetters("AaBbCc1234ABC");
        System.out.println("countUpperCaseLetters(\"AaBbCc1234ABC\") = " + countUpperCaseLetters("AaBbCc1234ABC"));
    }
}
  • 4
package com.github.hcsp.controlflow;

public class Main {
    public static void main(String[] args) {
        printNarcissisticNumber();
    }

    /**
     * 打印所有的水仙花数。水仙花数是指一个三位数,其各位数字立方和等于该数本身。
     *
     * <p>例如,153是一个水仙花数,因为1的立方+5的立方+3的立方 = 153
     *
     * <p>提示:可用除法和求余运算得到一个数字的个、十、百位上的数字。
     */
    public static void printNarcissisticNumber() {
        for (int i = 100; i < 1000; i++) {
            int g = i % 10;
            int s = i % 100 / 10;
            int b = i / 100;

            if (b * b * b + s * s * s + g * g * g == i) {
                System.out.println(i);
            }

        }
    }
}
  • 5
package com.github.hcsp.controlflow;

public class Main {
    public static void main(String[] args) {
        System.out.println(isSymmetric("1234"));
        System.out.println(isSymmetric("1234321"));
    }

    /**
     * 判断一个字符串是否是对称字符串。一个字符串对称意味着它和它的逆序相同。
     *
     * <p>例如,"12321"和"上海自来水来自海上"是对称字符串 "1234"不是对称字符串
     *
     * @param str 给定的字符串
     * @return 若给定的字符串是对称的,返回true,否则返回false
     */
    public static boolean isSymmetric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(str.length() - i - 1) != str.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}
  • 6
package com.github.hcsp.controlflow;

public class Main {
    public static void main(String[] args) {
        System.out.println(factorial(0));
        System.out.println(factorial(1));
        System.out.println(factorial(2));
        System.out.println(factorial(5));
    }

    /**
     * 计算一个数的阶乘。 阶乘的定义为: 0的阶乘为1 n的阶乘为n*(n-1)*(n-2)*...*2*1
     *
     * @param n 输入数字
     * @return 该数字的阶乘
     */
    public static int factorial(int n) {
        int j = 1;
        for (int i = 1; i < n; i++) {
            j = j * i;
        }
        return j;
    }
}

-7

package com.github.hcsp.controlflow;

public class Main {
    public static void main(String[] args) {
        System.out.println(howManyDaysInMonth(0));
        System.out.println(howManyDaysInMonth(1));
        System.out.println(howManyDaysInMonth(5));
        System.out.println(howManyDaysInMonth(9));
        System.out.println(howManyDaysInMonth(11));
    }

    /**
     * 输入一个月份,返回该月份在平年的天数或者"非法输入"
     *
     * <p>最好使用switch语句
     *
     * <p>例如,输入2,返回"2月有28天";输入12,返回"12月有31天"。若输入的是1~12之外的值,返回"非法输入"
     *
     * @param month 月份
     * @return 结果字符串
     */
    public static String howManyDaysInMonth(int month) {
        switch (month) {
            case 1: {
                return "1月有31天";
            }
            case 2: {
                return "2月有28天";
            }
            case 3: {
                return "3月有31天";
            }
            case 4: {
                return "4月有30天";
            }
            case 5: {
                return "5月有31天";
            }
            case 6: {
                return "6月有30天";
            }
            case 7: {
                return "7月有31天";
            }
            case 8: {
                return "8月有31天";
            }
            case 9: {
                return "9月有30天";
            }
            case 10: {
                return "10月有31天";
            }
            case 11: {
                return "11月有30天";
            }
            case 12: {
                return "12月有31天";
            }
            default: {
                return "非法输入";
            }
        }
    }
}

  • 8
package com.github.hcsp.controlflow;

public class Main {
    public static void main(String[] args) {
        System.out.println(howManyPrimeNumbers(5));
    }

    /**
     * 给定一个数字n,返回1到n之间的质数(素数)个数,不包括n本身。
     *
     * <p>例如,n=5,返回2,因为1到5之间有2个质数:2和3。
     *
     * <p>提示:对于正整数n,如果用2到Math.sqrt(n)+1之间的所有整数去除,均无法整除,则n为质数。
     *
     * @param n 给定的数字
     * @return 1到n之间(不包括n)质数的个数
     */
    public static int howManyPrimeNumbers(int n) {
        int k = 0;
        for (int i = 2; i < n; i++) {
            boolean flag = true;
            for (int j = 2; j < Math.sqrt(n) + 1; j++) {
                if ((i % j == 0) && (i != j)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                k += 1;
            }
        }
        return k;
    }
}

  • 9-1-9