4.16 第七周上机练习和作业
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
1 package sjw; 2 public class word1 { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 int a[]={10,20,30,40,50}; 9 for (int i = 0; i <5; i++) { 10 System.out.println(a[i]); 11 } 12 } 13 14 }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值
1 package sjw; 2 import java.util.Scanner; 3 public class work2 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 11 int []a=new int[5]; 12 Scanner input=new Scanner(System.in); 13 for (int i = 0; i < a.length; i++) { 14 a[i]=input.nextInt(); 15 } 16 for (int i = 0; i < a.length; i++) { 17 System.out.println("输出"+a[i]); 18 } 19 } 20 21 }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
1 package sjw; 2 public class work3 { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 char []a={'c','h','i','n','a'}; 10 for (int i = 0; i < a.length; i++) { 11 System.out.println(a[i]); 12 } 13 } 14 15 }
4.输入5个学生成绩,求总分和平均分
1 package sjw; 2 import java.util.Scanner; 3 public class work4 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int sum=0; 11 double ave=0; 12 int []a=new int[5]; 13 Scanner input=new Scanner(System.in); 14 for (int i = 0; i < a.length; i++) { 15 a[i]=input.nextInt(); 16 } 17 for (int i = 0; i < a.length; i++) { 18 sum+=a[i]; 19 } 20 ave=sum/5.0; 21 System.out.println("总成绩为"+sum); 22 System.out.println("平均成绩为"+ave); 23 } 24 25 }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
1 package sjw; 2 public class work53 { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 int []a={12,53,23,44,53}; 10 for (int i = 0; i < a.length; i++) { 11 System.out.println(a[i]); 12 } 13 for (int i = 4; i >=0; i--) { 14 System.out.println("倒叙输出"+a[i]); 15 } 16 } 17 18 }
1 package sjw; 2 public class work53 { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 int []a={12,53,23,44,53}; 10 for (int i : a) { 11 System.out.println(i); 12 } 13 for (int i = 4; i >=0; i--) { 14 System.out.println("倒叙输出"+a[i]); 15 } 16 } 17 18 }
作业:
1.定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
1 package sjw; 2 import java.util.Scanner; 3 public class work2 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int a[]=new int[5]; 11 Scanner input=new Scanner(System.in); 12 int j=0,k=0; 13 for (int i = 0; i < a.length; i++) { 14 a[i]=input.nextInt(); 15 } 16 for (int i = 0; i < a.length; i++) { 17 if(a[i]%2==1){ 18 j++; 19 } 20 if(a[i]%2==0){ 21 k++; 22 } 23 } 24 System.out.println("奇数个数为"+j); 25 System.out.println("偶数个数为"+k); 26 } 27 }
2.生成一个100长度数组,里面的数分别是1-100,并输出
1 package sjw; 2 public class work53 { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 int a[]=new int[100]; 10 for(int i=0;i<100;i++){ 11 a[i]=i+1; 12 System.out.println(a[i]); 13 } 14 } 15 }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
1 package sjw; 2 import java.util.Scanner; 3 public class work2 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 double a[]=new double[10]; 11 double sum=0; 12 Scanner input=new Scanner(System.in); 13 for (int i = 0; i < a.length; i++) { 14 System.out.println("输入第"+(i+1)+"个学生成绩"); 15 a[i]=input.nextDouble(); 16 } 17 for (int i = 0; i < a.length; i++) { 18 sum=a[i]+5; 19 if(sum>=100){ 20 System.out.println("加分后成绩为100分"); 21 } 22 else { 23 System.out.println("加5分后成绩为"+sum+"分"); 24 } 25 }} 26 }