第七周上机练习
1.
package test; import java.util.*; public class LH { public static void main(String[] args) { int []a=new int[10]; Scanner input=new Scanner(System.in); for (int i = 0; i < a.length; i++) { int x=input.nextInt(); a[i]=x; } 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=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } int sum=0; for (int i = 0; i < a.length; i++) { sum=a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]; } System.out.println(""+sum/8); } }
2.
package test; import java.util.Random; public class LH{ public static void main(String[] args) { int[] a = new int[10]; Random r = new Random(); for (int i = 0; i < 10; i++) { a[i] = r.nextInt(100); } 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 = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } System.out.println("排序后为:"); for (int i = 0; i < 10; i++) { System.out.println( a[i]); } } }
3.
package test; import java.util.Random; public class LH { 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.
import java.util.Scanner; public class LH { public static void main(String[] args) { int []arr= new int[10]; int a=0; int b=0; Scanner ac=new Scanner(System.in); System.out.println("请输入10个数:"); for(int i=0;i<arr.length;i++) { arr[i]=ac.nextInt(); } int max=arr[0]; int min=arr[0]; for(int i=0;i<arr.length;i++) { if(arr[i]>max) { max=arr[i]; }else{ min=arr[i]; } if(arr[i]%2==0) { b++; }else { a++; } } System.out.println("最大值为:"+max+"最小值为:"+min); System.out.println("奇数和为:"+a+"偶数和为:"+b); } }