4.16 java homework

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

package four;

import java.util.Scanner;

public class text {
    public static void main(String[] args) {
        int[] a=new int[10];
        int sum=0;
        Scanner sc= new Scanner(System.in);
        for(int i=0;i<a.length;i++) {
            System.out.println ( "Enter the number" + (i + 1) + " numbers " );
             int mark = sc.nextInt ();
            a[i]=mark;
        }
        for(int i=0;i<a.length-1;i++) {
            for(int j=0;j<a.length-1-i;j++) {
                if(a[j]>a[j+1]) {
                    int temp;
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j]=temp;
                }
            }
        }
        for(int i=1;i<a.length-1;i++) {
            sum+=a[i];
        }
        
        System.out.println ( "Output average" + sum / (a.length-2 ));
    }
}
    

 


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 four;

import java.util.Random;

public class text {
    public static void main(String[] args) {
        int[] a=new int[10];
        Random r=new Random();
        for(int i=0;i<a.length;i++) {
            int num=r.nextInt(90);
            a[i]=num;
        }
        for(int n:a) {
            System.out.println(n);
        }
    }
}
    

 


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

package four;

import java.util.Random;

public class text {
    public static void main(String[] args) {
        int []a=new int[7];
        Random r=new Random();
        for(int i=0;i<a.length;i++) {
            int num=r.nextInt(35)+1;
            a[i]=num;
        }
        for(int i=0;i<a.length;i++) {
            for(int j=0;j<a.length;j++) {
                while(a[i]==a[j]) {
                    if(i==j) {
                        break;
                    }
                    int num = r.nextInt (35) +1 ;
                    a[i]=num;
                }
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[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 four;

import java.util.Scanner;

public class text {
    public static void main(String[] args) {
        int []a=new int[10];
        int ou=0;
        int dan=0;
        Scanner sc=new Scanner(System.in);
        for(int i=0;i<a.length;i++) {
            System.out.println ( "The first" + (i + 1) + "number" in the input array );
             int num = sc.nextInt ();
            a[i]=num;
            if(a[i]%2==0) {
                or ++ ;
            }else {
                and ++ ;
            }
        }
        for(int i=0;i<a.length;i++) {
            for(int j=0;j<a.length-1-i;j++) {
                if(a[j]<a[j+1]) {
                    int temp;
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        System.out.println ( "The maximum value is" + a [0] + "minimum value" + a [a.length-1] + "Even number is" + ou + "Odd number is" + dan);
    }
}

 

Guess you like

Origin www.cnblogs.com/M1223631418/p/12710851.html