Java第7次作业
练习
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
1 package Third; 2 3 public class D1 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int[] a = { 10, 20, 30, 40, 50 }; 11 for (int i = 0; i < a.length; i++) { 12 System.out.println(a[i]); 13 } 14 15 } 16 }

2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
1 package Third; 2 3 import java.util.Scanner; 4 5 public class D2 { 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 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 System.out.println(a[i]); 19 } 20 21 } 22 }

3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
1 package Third; 2 3 public class D3 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 char[] a = { 'c', 'h', 'i', 'n', 'a' }; 11 for (int i = 0; i < a.length; i++) { 12 System.out.println(a[i]); 13 } 14 15 } 16 }

4.输入5个学生成绩,求总分和平均分
1 package Third; 2 3 import java.util.Scanner; 4 5 public class D4 { 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 int[] a = new int[5]; 13 int sum = 0; 14 int ave = 0; 15 Scanner input = new Scanner(System.in); 16 for (int i = 0; i < a.length; i++) { 17 a[i] = input.nextInt(); 18 sum = sum + a[i]; 19 ave = sum / a.length; 20 } 21 System.out.println("总和是" + sum + "平均数是" + ave); 22 } 23 24 }

5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出(for)
1 package Third; 2 3 public class D5 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int[] a = { 12, 53, 23, 44, 53 }; 11 System.out.println("for输出"); 12 for (int i = 0; i < a.length; i++) { 13 System.out.println(a[i]); 14 } 15 System.out.println("foreach输出"); 16 for (int i : a) { 17 System.out.println(i); 18 } 19 System.out.println("倒序输出"); 20 for (int i = a.length - 1; i >= 0; i--) { 21 System.out.println(a[i]); 22 } 23 24 } 25 }

作业:
1.定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
1 package ddd; 2 import java.util.Scanner; 3 public class test { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 System.out.println("输入五个数"); 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 int m=0; 17 int n=0; 18 for (int i = 0; i < a.length; i++) { 19 if(a[i]%2==0){ 20 m++; 21 }else{ 22 n++; 23 } 24 } 25 System.out.println("奇数个数为"+n+"个,"+"偶数个数为"+m+"个"); 26 } 27 28 }

2.生成一个100长度数组,里面的数分别是1-100,并输出
1 package gc1; 2 3 public class gc7 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int []a=new int[100]; 11 for (int i = 0; i < a.length; i++) { 12 a[i]=i+1; 13 } 14 for (int i = 0; i < a.length; i++) { 15 System.out.println(a[i]); 16 } 17 } 18 19 }

3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
1 package z6; 2 import java.util.Scanner; 3 public class test2 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 Scanner input = new Scanner(System.in); 11 System.out.println("输入十个学生的成绩"); 12 int []a=new int[10]; 13 for (int i = 0; i < a.length; i++) { 14 a[i]=input.nextInt(); 15 } 16 for(int j=0,i=0;j<=9;j++,i++){ 17 a[j]=a[i]+5; 18 if(a[j]>100){ 19 a[j]=100; 20 } 21 } 22 System.out.println("加五分后成绩为"); 23 for (int i = 0; i < a.length; i++) { 24 System.out.println(a[i]); 25 } 26 } 27 28 }




浙公网安备 33010602011771号