java7次

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

 1 package exp_001;
 2 
 3 public class num_org_001 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int a[]=new int[]{10,20,30,40,50};
 8         for (int i : a) {
 9             System.out.println(i);
10         }
11     }
12 
13 }


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

 1 package exp_001;
 2 
 3 import java.util.Scanner;
 4 
 5 public class num_org_002 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner input=new Scanner(System.in);
10         System.out.println("请为数组赋值(5)个");
11         int a[]=new int [5];
12         for (int i = 0; i < a.length; i++) {
13             a[i]=input.nextInt();
14         }
15         for (int i : a) {
16             System.out.println(i);
17         }
18         
19     }
20 
21 }


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

 1 package exp_001;
 2 
 3 public class num_org_003 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         String ch[]=new String[]{"C","h","i","n","a"};
 8         for (String string : ch) {
 9             System.out.print(string);
10         }
11     }
12 }


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

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


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

 1 package exp_001;
 2 
 3 public class num_org_005 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int a[]=new int[]{12,53,23,44,53};
 8         System.out.println("for");
 9         for (int i = 0; i < a.length; i++) {
10             System.out.print(a[i]+" ");
11         }
12         System.out.println("\nforeach");
13         for (int i : a) {
14             System.out.print(i+" ");
15         }
16         System.out.println("\n倒序输出");
17         for (int i = a.length-1; i>-1; i--) {
18             System.out.print(a[i]+" ");
19         }
20     }
21 
22 }


作业:


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

 1 package exp_001;
 2 
 3 import java.util.Scanner;
 4 
 5 public class num_work_001 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner input=new Scanner(System.in);
10         System.out.println("请为数组赋值(8个数)");
11         int a=0;
12         int b=0;
13         int c=0;
14         int num[]=new int[8];
15         for (int i = 0; i < num.length; i++) {
16             num[i]=input.nextInt();
17         }
18         for (int i = 0; i < num.length; i++) {
19             if(num[i]%2==0) {
20                 a++;
21             }
22             else {
23                 b++;
24             }
25         }
26         System.out.println("奇数个数"+a+"偶数个数"+b);
27     }
28 
29 }


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

 1 package exp_001;
 2 
 3 public class num_work_002 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("数组最值(10,20,30,40,50)\n求最大值和最小值");
 8         int a[]=new int[]{10,20,30,40,50};
 9         int max=a[0];
10         int min=a[0];
11         for (int i : a) {
12             if(i>max) {
13                 max=i;
14             }
15             if(i<min) {
16                 min=i;
17             }
18         }
19         System.out.println("最大值"+max+ "最小值"+min);
20     }
21     
22 
23 }


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

 1 package exp_001;
 2 
 3 import java.util.Scanner;
 4 
 5 public class num_work_003 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner input=new Scanner(System.in);
10         System.out.println("输入10个学生成绩[0,100]");
11         int sco[]=new int[10];
12         for (int i = 0; i < sco.length; i++) {
13             sco[i]=input.nextInt();
14         }
15         System.out.println("加分后的成绩");
16         for (int i : sco) {
17             if(i>=95) {
18                 System.out.print("100 ");
19             }
20             else {
21                 System.out.print(i+5+" ");
22             }
23         }
24     }
25 
26 }

posted @ 2021-04-20 16:24  计算机1903孙铭泽  阅读(37)  评论(0编辑  收藏  举报