Java学习第7天

2016-12-05  本文已影响0人  _Raye

- Java中创建包,一半用公司域名倒过来写的方法进行命名。

- Java方法的重载

private static final double AISLE_UNIT_PRICE = 38.5;
    private static final double F_UNIT_PRICE = 15.5;
    //LAP - 最小惊讶原则
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入游泳池半径:");      
        double radius = input.nextDouble(); 
        if (radius > 0) {
            System.out.printf("跑道的总造价为:¥%.2f元\n",total(radius));
        }
        else {
            System.out.println("游泳池的半径应该是一个正整数");
        }
        input.close();
    }
    
    public static double area( double radius) {
        return Math.PI * radius *radius;
    }
    public static double perimeter (double radius) {
        return 2 * Math.PI * radius;
    }
    public static double total(double radius) {
        double asislePrice = (area(radius + 3) - area(radius))* AISLE_UNIT_PRICE;
        double fencePrice = perimeter(radius) * F_UNIT_PRICE;
        double totalmoney = asislePrice + fencePrice;   
        return totalmoney;
        
    }

- 方法使用的实例

//计算组合数:C(m,n) = m! / n! * (m - n)!
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入m:");
        int m = input.nextInt();
        System.out.print("请输入n:");
        int n = input.nextInt();
        if (m >= n) {
            System.out.printf("组合数C(%d,%d) = %.0f",m,n,f(m) / f(n) / f(m - n));
        }
        else {
            System.err.println("输入错误!!!");
        }
        input.close();
    }
    public static double f(int n) {
        double fn = 1;
        for(int i = 2;i <= n;i++){
             fn *= i;
        }       
        return fn;
    }

- 数组

public static void main(String[] args) {
        int [] f = {0,0,0,0,0,0};//int[] f = new int[6];    表示数组里面有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] + "次");
        }
    }

- 斐波拉契数列

public static void main(String[] args) {
        long[] f = new long[50];
        f[0] = f[1] = 1;
        for (int i = 2; i < f.length; i++){//f.length表示数组的长度
            f[i] = f[i - 1] + f[i - 2];
        }
        for (int i = 0;i < f.length;i++){
            System.out.println(f[i]);   
        }
    }

- 数组的练习

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;
        for (int i = 0; i < scores.length;i++){
            sum += scores[i];
        }       
        double max = scores[0];
        double min = scores[0];
        for(int i = 1; i < scores.length;i++){          
            if(scores[i] > max){
                max = scores[i];
            }
            else if (scores[i] < min) {
                min = scores[i];
            }                       
        }   
        System.out.println("平均分:" + sum / scores.length);
        System.out.println("最高分为:" + max);
        System.out.println("最低分为:" + min);
    }

- Josephu环(约瑟夫环)

//练习:有30个人(15个基督教徒,15个非教徒)坐船,船坏了,要把15个人扔到海里,其他人才能得救
//围成一个圈,从某个人开始从1开始报数,报到9的人扔到海里,下一个继续从1开始报数
//直到把15个人扔到海里为止,结果15个基督教徒都幸免于难
//问这些人是怎么站的,哪些位置是基督教徒,哪些是非教徒 ---Josephu环(约瑟夫环)
public class Test06 {

    public static void main(String[] args) {
        boolean[] persons = new boolean[30];//默认值是false
        for(int i = 0; i < persons.length; i++){
                persons[i] = true;
            }
        int counter = 0;//弄死多少个人
        int index = 0;//操作数组的下标
        int number = 0;//报数的值
        while (counter < 15) {
            if (persons[index]) {
                number += 1;
                if (number == 9) {
                    persons[index] = false;
                    counter += 1;
                    number = 0;
                }
            }
            index += 1;
            index %= persons.length;//防止越界
        }
        for (boolean isChrist: persons){
            System.out.print(isChrist ? "基" : "非");
        }
    }
}

- 冒泡排序

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 + " ");
        }
    }

    private static void bubblesort(int[] array) {
        boolean swapped = true;
        for(int i = 1; swapped && i < array.length; 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;
                }
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读