4.3总结

发布时间 2023-04-03 20:25:57作者: 封织云

1、信号位:(立的flag) boolean flag = ture;//一开始认为当前数据是(素数);素数问题解决。
一、找素数

package com.itzihan.demo;

public class Test2 {
public static void main(String[] args) {
//1、定义一个循环,找到101到200的数
//2、判断当前遍历的数据是否是素数
//3、根据判定是否输出这个数据

    for (int i = 101; i <= 200; i++) {

        boolean flag = true;

        for (int j = 2; j < i / 2; j++) {
            if (i % j == 0){
                flag = false;
                break;
            }

        }

        if (flag){
            System.out.println(i + "\t\n");
        }
    }

}

}

————————————————————

二、复制数组

package com.itzihan.demo;

//需求:把一个数组的元素复制到另一个数组。

/**

  • 分析:1、需要一个动态初始化数组,长度与原数组一样
  •  2、遍历数字每个元素,依次赋值给新数组。
    
  •  3、输出两个数组的内容。
    

*/

public class Test3 {

//定义一个方法,
public static void main(String[] args){
    int[] arr1 = {11 , 22 , 33 , 44};
    //int [] arr2 = arr1;   不是数组的复制

    int[] arr2 = new int[arr1.length];

    copy(arr1 , arr2);

    printArray(arr1);
    printArray(arr2);

}

public static void printArray(int[] arr){
    System.out.print("[");
    for (int i = 0; i < arr.length; i++) {
        System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
        
    }
    System.out.println("]");

}

public static void copy(int[] arr1, int[] arr2){
    //完成数组的复制
    for (int i = 0; i < arr1.length; i++) {
        arr2[i] = arr1[i];
    }

}

}

——————————————————————————————————————————————

三、评委打分

package com.itzihan.demo;

import java.util.Scanner;

public class Test4 {

//定义一个动态初始化的数组,用于后期6个评委的分数
public static void main(String[] args) {
    int[] scores = new int[6];

    //2、录入六个评委的分数

    Scanner sc = new Scanner(System.in);
    for (int i = 0; i < scores.length; i++) {
        System.out.println("请您输入第" + (i + 1) + "个评委打分");
        int score = sc.nextInt();
        //将分数存入数组
        scores[i] = score;
    }

    //3、遍历数组,找出最大值,最小值,总分
    int max = scores[0];
    int min = scores[0];
    int sum = 0;
    for (int i = 0; i < scores.length; i++) {
        if (scores[i] >= max) {
            //替换最大值
            max = scores[i];
        }
        if (scores[i] <= min) {
            //替换最大值
            min = scores[i];
        }
        sum += scores[i];
    }

    System.out.println("最高分是" + max);
    System.out.println("最低分是" + min);
    System.out.println("去除最高分和最低分后");
    //计算平均分
    double result =  (sum - max - min) * 1.0 / (scores.length - 2);
    System.out.println("选手的最终得分是" + result);


}

}

————————————————————————————————————————————————————————————————

四、双色球系统