美文网首页
Java学习1205

Java学习1205

作者: a04b20daaf33 | 来源:发表于2016-12-05 20:43 被阅读0次
    • 捕鱼问题
    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] + "次");
            }
        }
    }
    
    
    • for-each只读循环,不能改变数组的元素
      (在for循环里面用x代表数组里的所有的值)
    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);
        }
    
    }
    
    
    • 15个基督徒和15个非基督徒从1开始报数,报到9的死,下一个人接到1开始报数。
    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;
                    }
                }
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:Java学习1205

          本文链接:https://www.haomeiwen.com/subject/qxrlmttx.html