Java学习笔记 - 第015天

2016-12-17  本文已影响0人  迷茫o

思维导图

Java基础.jpg

例子

        int i = 0;
        for (System.out.println('a'), System.out.println('b');
                i < 3;
                i++, System.out.println('c')) {
            if (i == 2) continue;
            System.out.println('d');
        }
        
        int a = 5, b = 10;
        boolean flag = true;
        // 模拟do...while功能
        while (flag || a > b) {
            flag = false;
        }
    public static void bubbleSort(int[] x) {
        boolean swapped = true;
        for (int i = 1; swapped && i < x.length; i++) {
            swapped = false;
            for (int j = 0; j < x.length - i; j++) {
                if (x[j] > x[j + 1]) {
                    int temp = x[j + 1];
                    x[j + 1] = x[j];
                    x[j] = temp;
                    swapped = true;
                }
            }
        }
    }
    
    public static int search(int[] x, int k) {
        for (int i = 0; i < x.length; i++) {
            if (x[i] == k) {
                return k;
            }
        }
        return -1;
    }
    
    public static void main(String[] args) {
        int[] array = new int[40];
        
        for (int i = 0, len = array.length; i < len; i++) {
            array[i] = (int) (Math.random() * 100);
        }
        System.out.println(Arrays.toString(array));
//      System.out.println(search(array, 25));
//      System.out.println(search(array, 62));
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
        Scanner input = new Scanner(System.in);
        System.out.print("输入正整数: ");
        int n = input.nextInt();
        int sum = 0;
        while (n > 0) {
            /*int temp = n;
            temp %= 10;
            n /= 10;
            sum += temp;*/
            sum += n % 10;
            n /= 10;
        }
        System.out.println(sum);
        input.close();
    public static int sum(int n) {
        if (n == 1) {
            return 1;
        }
        return n + sum(n - 1);
    }
    
    public static double f(int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        return n * f(n - 1);
    }
    
    public static void main(String[] args) {
        System.out.println(f(5));
        System.out.println(sum(5));
    }
    public static int steps(int n) {
        if (n < 0) {
            return 0;
        }
        else if (n == 0) {
            return 1;
        }
        return steps(n - 1) + steps(n- 2) + steps(n - 3);
    }
    
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(steps(i));
        }
    }
    public static void hanoi(int n, char a, char b, char c) {
        if (n > 0) {
            hanoi(n - 1, a, c, b);
            System.out.println(a + "--->" + b);
            hanoi(n - 1, c, b, a);
        }
    }
    
    public static void main(String[] args) {
        hanoi(3, 'A', 'B', 'C');
    }

作业

        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个正整数: ");
        int n = input.nextInt();
        for (int i = n / 2; i > 1; i--) {
            boolean isPrime = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                while (n % i == 0) {
                    n /= i;
                    System.out.println(i);
                }
            }
        }
        input.close();
    }
上一篇下一篇

猜你喜欢

热点阅读