Java学习1205

2016-12-05  本文已影响0人  a04b20daaf33
package com.baidu;

public class Test01 {

    public static void main(String[] args) {
        for (int i = 1; ; i++) {
            int total = i;
            boolean isEnough = true;
            for (int j = 1; j <= 5; j++) {
                if((total - 1) % 5 == 0){
                    total = (total - 1) / 5 * 4;
                }
                else {
                    isEnough = false;
                    break;
                }
            }
            if(isEnough){
                System.out.println(i);
                break;
            }
        }

    }

}

package com.baidu;

import java.util.Scanner;

//  排列数:  A(m,n) = m! / n!
//  组合数: C(m,n) = m! / n! / (m-n)!
public class Test03 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("输入两个正整数: ");
        int a = input.nextInt();
        int b = input.nextInt();
        if(a >= b){
        int fa = f(a);
        int fb = f(b);
        int fc = f(a-b);
        int x = fa / fb / fc;
        System.out.println(x);
        }
        else{
            System.out.println("a必须大于b!");
        }
        input.close();

    }
//    重载方法  
//    在一个类中可以出现同名方法  只要他们的参数列表不同就能够加以区分
//    参数列表不同指的是: 参数的类型不相同或者参数的个数不相同或者二者皆不同
    public static int f(int n) {
        int fb = 1;
        for (int j = n; j > 0; j--) {
            fb *= j;
        }
        return fb;
    }

}

package com.baidu;

public class Test05 {

    public static void main(String[] args) {
        //数组 - 用一个变量保存多个同类型的值
        int[] f = { 0, 0, 0, 0, 0, 0 };// int[] f = new int[6]
        for (int i = 1; i <= 60000; i++) {
            int face = (int) (Math.random() * 6 + 1);
            f[face - 1] += 1;
        }
        for (int i = 1; i <= 6; i++) {
            System.out.println(i + "点出现了" + f[i - 1] + "次");
        }
    }
}

package com.baidu;

public class Test06 {

    public static void main(String[] args) {
        int[] f = new int[20];
        f[0] = f[1] = 1;
        for (int i = 2; i < f.length; i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
        // for-each循环(Java 5+)
        // 只读循环,不能改变数组的元素
        for (int x : f) {
            System.out.println(x);
        }
    }

}

package com.baidu;

import java.util.Scanner;

public class Test07 {

    public static void main(String[] args) {
        String[] names = {"关羽","张飞","刘备","赵云","马超"};
        double[] scores = new double[names.length];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            System.out.print("请输入" + names[i] + "的成绩 :");
            scores [i] = input.nextDouble();
        }
        input.close();
        double sum = 0;
        double min = scores[0];
        double max = scores[0];
        for (int i = 0; i < scores.length; i++) {
            sum += scores[i];
        }
        for (int i = 1; i < scores.length; i++) {
            if(min > scores[i]){
                min = scores[i];    
            }
            else if(max < scores[i]){
                max = scores[i];    
            }
        }
        System.out.println("平均分:" + sum / scores.length);
        System.out.println("最低分: " + min);
        System.out.println("最高分: " + max);
    }

}

package com.baidu;

public class Test09 {

    public static void main(String[] args) {
        boolean[] people = new boolean[30];
        for (int i = 0; i < people.length; i++) {
            people[i] = true;
        }
        int counter = 0;
        int index = 0;
        int number = 0;
        while(counter < 15){
            if(people[index]){
                number += 1;
                if(number == 9){
                    people[index] = false;
                    counter += 1;
                    number = 0;             
                }
            }
            index += 1;
            if(index == people.length){
                index = 0;
            }
        }
        for(boolean isChrist : people){
            System.out.println(isChrist ? "基督徒" : "非基督徒");
        }

    }

}

package com.baidu;

public class Test10 {

    public static void main(String[] args) {
        int[] x = {23 , 67 , 12 , 99 , 58 , 77 , 88 , 4 , 45 , 81};
        bubbleSort(x);
        for(int a : x){
            System.out.print(a + "");
        }
    }

    public static void bubbleSort(int[] array) {
        boolean swapped = true;
        for (int i = 1; swapped && i <= array.length - 1; i++) {
            swapped = false;
            for(int j = 0 ; j < array.length - i; j++){
                if(array [j] > array [j + 1]){
                    //交换两个元素
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped= true;
                }
            }
        }
    }

}

上一篇 下一篇

猜你喜欢

热点阅读