The sixth week homework exercises

1. There are 10 judges to score, (remove one highest and one lowest) to get the average score.

package calsswork;
import java.util.Scanner;
public class text1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a[] = new int[10];
        double sum = 0;
        int b = 1;
        double c = 0;
        for (int i = 0; i < a.length; i++) {
            System.out.println("输入数组" );
            a[i] = sc.nextInt();
        }
        for (int x = 0; x < a.length - 1; x++) {
            for (int y = 0; y < a.length - 1 - x; y++) {
                if (a[y] > a[y + 1]) {
                    int temp = a[y + 1];
                    a[y + 1] = a[y];
                    a[y] = temp;
                }
            }
        }
        for (int n : a) {
            System.out.println(n + "");
        }
         
        for (; b < a.length - 1; b++) {
            sum += a[b];
            c = sum / b;
        }
        System.out.println("平均数:" + c);
    }
}

 

2. Learn about Java random numbers by yourself, generate a random array of length 10 (each number ranges from 0 to 99), and output after sorting.

package calsswork;
import java.util.Scanner;
public class text1 {
    public static void main(String[] args) {
        int arr[] = new int[10];
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt(100);
            System.out.print(arr[i] + ", ");
        }
        System.out.println();
        System.out.println("随机数开始排序!!!");
        for (int x = 0; x < arr.length - 1; x++) {
            for (int y = 0; y < arr.length - 1 - x; y++) {
                if (arr[y] > arr[y + 1]) {
                    int temp = arr[y + 1];
                    arr[y + 1] = arr[y];
                    arr[y] = temp;
                }
            }
 
        }
        for (int n : arr) {
            System.out.println(n + ", ");
        }
 
    }
 
}

 

3. Make 7 out of 35 lottery programs. (That is, 1 ~ 35 randomly generates 7 non-repeating numbers)

package calsswork;
import java.util.Random;
public class text1 {
    public static void main(String[] args) {
                int[] a= new int[35];
                Random r=new Random();
                for (int i = 0; i < a.length; i++) {
                    a[i]=r.nextInt(34)+1;
                }
                int[] b=new int[7];
                for (int j = 0; j < 7; j++) {
                    b[j]=a[r.nextInt(36)];
                }
                for (int i : b) {
                    System.out.print(i+"  ");
                }
            }
        }

 

4. Define an int array with a length of 10, and count the maximum, minimum, and odd and even numbers in the array

package calsswork;
import java.util.Scanner;
public class text1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int arr[] = new int[10];
        int js = 0, os = 0;
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个数:");
            arr[i] = sc.nextInt();
        }
        int max = arr[0], min = arr[0];
        for (int x = 1; x < arr.length; x++) {
            if (arr[x] > max) {
                max = arr[x];
            } 
            else if (arr[x] < min) {
                min = arr[x];
            }
        }
        for (inty = 0 ; y <arr.length; y ++ ) {
             if (arr [y]% 2 == 0 ) { 
                os ++ ; 
            } 
            else { 
                js ++ ; 
            } 
        } 
        System. out .println ( "The maximum value is: " + max + " , " + " The minimum value is: " + min); 
        System. out .println ( " The odd number is: " + js + ", " + " The even number is: " + os); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/jiming123/p/12714108.html