Java第五次作业

  1. 编写一个简单程序,要求数组长度为5,静态赋值1020304050,在控制台输出该数组的值
  1.  1 package tk;
     2 
     3 public class work {
     4 
     5     public static void main(String[] args) {
     6 
     7         int[] input=new int[]{10,20,30,40,50};
     8 
     9         for(int i=0;i<input.length;i++){
    10 
    11             System.out.print(input[i]+" ");
    12 
    13         }
    14 
    15     }
    16 
    17 }

     

    2、编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
 1 package apex;
 2 
 3 public class apex {
 4 
 5  
 6 
 7     public static void main(String[] args) {
 8 
 9         // TODO Auto-generated method stub
10 
11         int[] a = new int[5];
12 
13         a[0] = 1;
14 
15         a[1] = 2;
16 
17         a[2] = 3;
18 
19         a[3] = 4;
20 
21         a[4] = 4;
22 
23         for (int i = 0; i < a.length; i++)
24 
25             System.out.println(a[i]);
26 
27     }
28 
29 }

 

 3、定义字符型数组,分别存储ch、 in并在控制台输出

  1.  1 package apex;
     2 
     3 public class apex {
     4 
     5  
     6 
     7     public static void main(String[] args) {
     8 
     9         // TODO Auto-generated method stub
    10 
    11         char[] a ={'c','h','i','n','a'};
    12 
    13         for (int i = 0; i < a.length; i++)
    14 
    15             System.out.println(a[i]);
    16 
    17     }
    18 
    19 }

     

    4.输入5个学生成绩,求总分和平均分
 1 package apex;
 2 
 3 import java.util.Scanner;
 4 
 5 public class apex {
 6 
 7  
 8 
 9     public static void main(String[] args) {
10 
11         // TODO Auto-generated method stub
12 
13         int[] a = new int[5];
14 
15         Scanner sc = new Scanner(System.in);
16 
17         for (int i = 0; i < a.length; i++) {
18 
19             System.out.println("请输入第" + (i + 1) + "学生成绩");
20 
21             a[i] = sc.nextInt();
22 
23         }
24 
25         System.out.println("总分");
26 
27         int sum = 0;
28 
29         for (int j = 0; j < a.length; j++) {
30 
31             sum += a[j];
32 
33         }
34 
35         System.out.println(sum);
36 
37         System.out.println("平均分");
38 
39         double x = sum / 5;
40 
41         System.out.println(x);
42 
43     }
44 
45 }

 

  1. 5、定义数组{12,53,23,44,53} forforeach分别输出,再倒序输出
 1 package apex;
 2 
 3 import java.util.Scanner;
 4 
 5 public class apex {
 6 
 7  
 8 
 9     public static void main(String[] args) {
10 
11         // TODO Auto-generated method stub
12 
13         int[] a ={12,53,23,44,53};
14 
15         for (int i = 0; i < a.length; i++) 
16 
17         System.out.println(+a[i]);
18 
19         System.out.println();
20 
21         for (int i = a.length-1; i >=0; i--) 
22 
23             System.out.println(a[i]);
24 
25     }
26 
27 }

 

6定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数

 1 package apex;
 2 
 3 import java.util.Scanner;
 4 
 5 public class apex {
 6 
 7  
 8 
 9     public static void main(String[] args) {
10 
11         // TODO Auto-generated method stub
12 
13         Scanner input = new Scanner(System.in);
14 
15         System.out.println("输入十个数");
16 
17         int x = 0;
18 
19         int y = 0;
20 
21         int[] z = new int[10];
22 
23         for (int i = 0; i < z.length; i++) {
24 
25             z[i] = input.nextInt();
26 
27             if (z[i] % 2 == 0) {
28 
29                 x++;
30 
31             }
32 
33             else {
34 
35                 y++;
36 
37             }
38 
39         }
40 
41         System.out.println("奇数" + y);
42 
43         System.out.println("偶数" + x);
44 
45     }
46 
47 }

 

 

 

7.生成一个100长度数组,里面的数分别是1-100,并输出
package ljh;

import java.util.Scanner;

public class dsa {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[]a=new int[100];

for (int i = 0; i < a.length; i++) {

a[i]=i+1;

System.out.println(a[i]);

}

}

 

}

 

 

 

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

package ljh;

import java.util.Scanner;

public class dsa { 

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input=new Scanner(System.in);

System.out.println("请输入10位学生成绩:");

double[]a=new double[10];

for (int i = 0; i < a.length; i++) {

a[i]=input.nextDouble();

if (a[i]<95) {

a[i]+=5;

} else {

a[i]+=5;

}

}

System.out.println("加分后成绩为:");

for (int i = 0; i < a.length; i++) {

System.out.println(a[i]);

}

}

 

}

 

posted @ 2023-05-21 15:37  coldlane  阅读(7)  评论(0编辑  收藏  举报