Java学习1205

  • 捕鱼问题
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;
                }
            }
        }
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,270评论 0 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,992评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,778评论 18 399
  • 认识小二是在把专业从转完的一个月之后,第一个学期看了大半个学期的美剧,硬是把一部十季的电视连片头片尾都不落的看完...
    单陈阅读 349评论 0 1
  • 每年6月份一批批的高考的莘莘学子怀着忐忑不安的心情迎接高考的到来,同时也有一大批的毕业生涌入社会,到处去找工作,在...
    简竹轩阅读 138评论 0 0