1. There are 10 judges to score, (remove one highest and one lowest) to get the average score.
import java.util.*; public class demo5 { public static void main(String[] args) { Scanner s = new Scanner(System.in); double[] arry = new double[10]; System.out.println ( "Please enter the score of 10 judges" ); for ( int i = 0; i <10; i ++ ) { arry [i] = s.nextDouble (); // Fraction } // sort the array from big to small double temp = 0 ; for ( int i = 0; i <arry.length; i ++ ) { for ( int j = 0; j <arry.length-i-1; j ++ ) { if (arry [j] <arry [j + 1 ]) { temp = arry[j]; arry[j] = arry[j+1]; arry [j+1] = temp; } } } // Traverse the new array // for (int i = 0; i <10; i ++) { // System.out.print (arry [i]); // } // Let the maximum value be 0 arry [0 ] = 0 ; arry[9]=0; double sum=0,pjs; for (int i = 0; i < arry.length; i++) { sum+=arry[i]; } pjs=sum/8; System.out.println ( "His average score is" + pjs); } }
2. Generate a random array of length 10 (each number ranges from 0 to 99), output after sorting.
import java.util.*; public class demo5 { public static void main(String[] args) { Random r = new Random (); // define random number int arry [] = new int [10 ]; // input random number in array for ( int i = 0; i <arry.length; i ++ ) { arry[i] = r.nextInt(100); } // Sort the array from large to small int temp = 0 ; for ( int i = 0; i <arry.length; i ++ ) { for ( int j = 0; j <arry.length-i-1; j ++ ) { if (arry [j] <arry [j + 1 ]) { temp = arry[j]; arry[j] = arry[j+1]; arry [j+1] = temp; } } } // Traverse the new array for ( int i = 0; i <10; i ++ ) { System.out.print(arry[i]+" "); } } }
3. Make 7 out of 35 lottery programs. (That is, 1 ~ 35 randomly generates 7 non-repeating numbers)
import java.util.*; public class demo5 { public static void main(String[] args) { Random r = new Random (); // define random number int arry [] = new int [7 ]; int t = 0 ; ban:for (int i = 0; i < arry.length; i++) { arry[i]=r.nextInt(35)+1; t=arry[i]; for (int j = 0; j < i; j++) { if(arry[j]==t) { i--; continue ban; } } } for (int i = 0; i < arry.length; i++) { System.out.print(arry[i]+" "); } } }
4. Define an int array of length 10 (if there is no special instruction, static assignment and dynamic assignment can be used), statistics of the maximum and minimum values in the array, and the number of odd and even numbers
public class demo5 { public static void main(String[] args) { int[] a = new int[]{2,3,52,3,69,58,46,12,53,27}; int max=a[0]; int mini=a[0]; int jishu=0; int oushu=0; for (int i = 1; i <a.length ; i++) { if (a[i]>max){ max=a[i]; } } for (int j = 1; j <a.length ; j++) { if (a[j]<mini){ mini=a[j]; } } for (int k = 0; k< a.length; k++) { if (a[k]%2==0){ oushu + = 1 ; }else { jishu + = 1 ; } } System.out.println ( "The maximum value is" + max + "The minimum value is" + mini + "Even numbers have" + oushu + "pieces" + "Odd numbers have" + jishu + "pieces" ); } }