1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
1 package dfishf; 2 import java.util.Scanner; 3 public class wwww { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner input=new Scanner(System.in); 8 System.out.println("输入数组长度"); 9 int i=input.nextInt(); 10 int[] sz={10,20,30,40,50}; 11 System.out.println("输出数组值为:"); 12 System.out.println(sz[i]); 13 } 14 }
2.编写一个简单程序,要求数组长度为5,动态赋值10,20,30,40,50,在控制台输出该数组的值。
1 package dfishf; 2 import java.util.Scanner; 3 public class wwww { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner input=new Scanner(System.in); 8 System.out.println("输入数组长度"); 9 int i=input.nextInt(); 10 int[] sz=new int[5]; 11 sz[0]=10; 12 sz[1]=20; 13 sz[2]=30; 14 sz[3]=40; 15 sz[4]=50; 16 System.out.println("数组值为:"+sz[i]); 17 } 18 }
3.编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值
package dfishf; public class wwww { public static void main(String[] args) { // TODO Auto-generated method stub int[] sz={23,45,22,33,56}; double sum=0; for(int i=0;i<=4;i++){ sum+=sz[i]; }System.out.println("数组和为:"+sum+"平均值为:"+sum/5); } }
4.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。
1 package dfishf; 2 3 public class wwww { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int[] sz={18,25,7,36,13,2,89,63}; 8 int max,maxdx=0; 9 max=sz[0]; 10 for(int i=1;i<=7;i++){ 11 if(sz[i]>max){ 12 max=sz[i]; 13 maxdx=i; 14 } 15 }System.out.println("最大值为:"+max+"下标为:"+maxdx); 16 } 17 }
5. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
1 package dfishf; 2 3 public class wwww { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int[] sz=new int[]{22,11,66,55}; 8 int t; 9 for(int i=0;i){ 10 t=sz[i]; 11 sz[i]=sz[sz.length-1-i]; 12 sz[sz.length-1-i]=t; 13 }System.out.println("输出倒序为:"); 14 for(int i=0;i ){ 15 System.out.println(sz[i]); 16 } 17 } 18 }