1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值
package com.itheima01; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int [] qwq = new int [5]; for(int i =0;i<5;i++){ qwq [i] = i*10+10; System.out.print(qwq[i]+" "); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值10,20,30,40,50,在控制台输出该数组的值
package com.itheima01; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int[] qwq=new int[5]; for (int i = 0; i < qwq.length; i++) { qwq[i] = sc.nextInt(); } for (int a : qwq) { System.out.println(a); } } }
3.编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值
package com.itheima01; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int [] qwq = new int[]{23,45,22,33,56}; int sum=0,pj=0; for (int i = 0; i < qwq.length; i++) { sum+=qwq[i]; } System.out.println("和是"); System.out.println(sum); pj=sum/5; System.out.println("平均值是"); System.out.println(pj); } }
4.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。
package com.itheima01; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int[] arr={18,25,7,36,13,2,89,63}; int max=arr[0]; int index = 0; for (int i = 0; i < arr.length-1; i++) { if (max]){ max=arr[i+1]; index=i+1; } } System.out.println("最大值为;"+max+"下标为"+index); } }
5. 将一个数组中的元素逆序存放
package com.itheima01; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int [] a = new int [] {4,1,3,5,9,2,1}; int t; for(int i = 0; i < a.length / 2; i ++) { t = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = t; } for(int i = 0; i < a.length; i ++) { System.out.print(a[i] + " "); }} }