Java第七次作业

上机练习

1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。

 1 package test;
 2 
 3 public class Lwk20 {
 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 : a) {
12             System.out.println(i);
13         }
14     }
15 
16 }

2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Lwk21 {
 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 : a) {
18             System.out.println(i);
19         }
20 
21     }
22 
23 }

3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出

 1 package test;
 2 
 3 public class Lwk22 {
 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 (char c : a) {
12             System.out.println(c);
13         }
14     }
15 
16 }

4.输入5个学生成绩,求总分和平均分

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Lwk23 {
 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         double s = 0;
15         Scanner input = new Scanner(System.in);
16         for (int i = 0; i < a.length; i++) {
17             a[i] = input.nextInt();
18             sum += a[i];
19         }
20         s = sum / 5.0;
21         System.out.println("总成绩为" + sum + "平均分为" + s);
22     }
23 
24 }

5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出

 1 package test;
 2 
 3 public class Lwk24 {
 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         for (int i = 0; i < a.length; i++) {
12             System.out.print(a[i] + " ");
13         }
14         System.out.println("");
15         for (int i : a) {
16             System.out.print(i + " ");
17         }
18         System.out.println("");
19         for (int i = a.length - 1; i >= 0; i--) {
20             System.out.print(a[i] + " ");
21         }
22     }
23 
24 }

作业:
1.定义一个整型数组,赋值后求出奇数个数和偶数个数

 1 package test;
 2 
 3 public class Lwk25 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int[] a = { 1, 2, 8, 5, 6, 11, 56, 55, 84, 91 };
11         int m = 0;
12         int n = 0;
13         for (int i = 0; i < a.length; i++) {
14             if (a[i] % 2 == 0) {
15                 m++;
16             } else {
17                 n++;
18             }
19         }
20         System.out.println("偶数的个数为" + m + "奇数的个数为" + n);
21     }
22 
23 }

2.定义一个数组,求数组中的最大值和最小值

 1 package test;
 2 
 3 public class Lwk26 {
 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 : a) {
15             System.out.println(i);
16         }
17     }
18 
19 }

3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Lwk27 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         double[] a = new double[10];
14         for (int i = 0; i < a.length; i++) {
15             a[i] = input.nextDouble();
16             a[i] += 5;
17             if (a[i] >= 100) {
18                 a[i] = 100;
19             }
20         }
21         for (double d : a) {
22             System.out.println(d);
23         }
24 
25     }
26 
27 }

posted @ 2021-04-17 20:47  Lwk36  阅读(47)  评论(0)    收藏  举报