Java 数组练习题2018.3.22

时间:2023-02-24 07:30:21
1. 写一个函数,计算一个整数数组的平均值
public class HomeWork322 {
	public static double avg(int[] a) {
		int sum=0;
		int avg=0;
		for(int i=0;i<a.length;i++) {
			sum+=a[i];
		}
		avg=sum/a.length;
		return avg;
	}
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入想要创建数组的内存:");
		int j = input.nextInt();
		System.out.println("请输入想要计算的数组值:");
		int[] a = new int[j];
		for(int i =0;i<a.length;i++) {
		 a[i]=input.nextInt();
		}
		int avg = HomeWork322.avg(a);
		System.out.println(avg);
	}
2.自定义一个整数数组a,读入一个整数n,
	如果n在数组中存在,则输出n的下标;如果不存在,则输出-1。
public class HomeWork322 {	
	public static boolean c(int n,int[] a) {
		boolean isRight=true;
		for(int i =0;i<a.length;i++) {
			if(a[i]==n) {
				isRight = false;
				System.out.println(i);
			}
		}
		return isRight;
	}
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入想要创建数组的内存:");
		int j = input.nextInt();
		System.out.println("请输入想要计算的数组值:");
		int[] a = new int[j];
		for(int i =0;i<a.length;i++) {
		 a[i]=input.nextInt();
		}
		System.out.println("请输入想要查找的数字:");
		int n = input.nextInt();
		boolean isRight = HomeWork322.c(n, a);
		 if(isRight) {
			 System.out.println("-1");
		 }
	}
}
3.给定一个数组,输出数组中的最大值和最小值	
	public static int[] m(int[] a) {
		int max = a[0];
		int min = a[0];
		for(int i =1;i<a.length;i++) {
			if(a[i]>max){
				max = a[i];
			}
			if(a[i]<min) {
				min = a[i];
			}
		}int[] m = {max,min};
		return m;
	}
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入想要创建数组的内存:");
		int j = input.nextInt();
		System.out.println("请输入想要计算的数组值:");
		int[] a = new int[j];
		for(int i =0;i<a.length;i++) {
		 a[i]=input.nextInt();
		}
		int[] m=HomeWork322.m(a);
		for(int i =0;i<m.length;i++) {
			System.out.println(m[i]);
		}
	}
}
4.给定一个数组,把这个数组中所有元素顺序进行颠倒。
	
	//方法一//
	public static int[] p(int[] a) {
		int[] p = new int[a.length];
			int j = a.length-1;
		for(int i =0;i<a.length;i++) {
			p[j]=a[i];
			j--;
		}
		return p;
	}
	//方法二:更快捷//
	public static int[] a(int[] a) {
		for(int star=0, end=a.length-1;star<end;star++,end--) {
			int temp = a[star];
			a[star] = a[end];
			a[end] = temp;
		}
		return a;
	}
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入想要创建数组的内存:");
		int j = input.nextInt();
		System.out.println("请输入想要计算的数组值:");
		int[] a = new int[j];
		for(int i =0;i<a.length;i++) {
		 a[i]=input.nextInt();
		}
		int[] p = HomeWork322.a(a);
		for(int i =0;i<a.length;i++) {
		System.out.println(p[i]);
		}
	}

5.完成数组的冒泡排序算法:给定一个数组:int[] a = {1,3,2,7,5},
	利用冒泡排序对其按照从小到大的顺序排序,然后输出结果。
	
	public static int[] c(int[] a) {
		        int len = a.length;
		        int temp;
		        for(int i = 0;i<len;i++)
		        {
		            for(int j = 0;j<len-i-1;j++)
		            {
		                if(a[j]>a[j+1])
		                {
		                    temp = a[j];
		                    a[j] = a[j+1];
		                    a[j+1] = temp;
		                }
		            }
		        }
		        return a;
		    }
	public static void main(String[] args) {
		int[] a = {1,3,2,7,5,3,2,5,4,4};	
		int[] c = HomeWork322.c(a);
		for(int i = 0;i<c.length;i++) {
			System.out.println(c[i]);
		}
	}
6.已知2个一维数组:a[]={3,4,5,6,7},b[]={1,2,3,4,5,6,7};把数组a与数组b
	/对应的元素乘积再赋值给数组b,如:b[2]=a[2]*b[2];最后输出数组b的元
	
	public static int[] s(int[] a,int [] b) {
		for(int i = 0;i<a.length;i++) {
			b[i]*=a[i];
		}
		return b;
	}
	
	public static void main(String[] args) {
		int[] a = {3,4,5,6,7};
		int[] b = {1,2,3,4,5,6,7};
		int[] s = HomeWork322.s(a, b);
		for(int i = 0;i<b.length;i++) {
			System.out.println(s[i]);
		}
	}
}