java第七周作业

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

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

  

 

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;
 
 
public class test {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
int []a=new int[5];
Scanner input=new Scanner(System.in);
for (int i = 0; i < a.length; i++) {
    a[i]=input.nextInt();
}   for (int i = 0; i < a.length; i++) {
    System.out.println(a[i]);
}
    }  
}

  

 

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
public class test {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char a[] = { 'c''h''i''n''a' };
        for (char i : a) {
            System.out.println(i);
        }
    }
}

  

 

 

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

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

  

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class test {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            int[] a = new int[] { 1253234453 };
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("-------------------");
        for (int i : a) {
            System.out.println(i);
        }
        System.out.println("-------------------");
        for (int i = 4; i >= 0; i--) {
            System.out.println(a[i]);
        }
 
    }
}

 

  

 

 

作业

  1. 定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    public class test {
     
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a[] = new int []{12,53,23,44,70};
            int o = 0,j = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i]%2==0) {
                o++;
            }
            if (a[i]%2!=0) {
                j++;
            }
        }
        System.out.println("偶数的个数是"+o);
        System.out.println("奇数的个数是"+j);
        }
     
    }

      

     

     2生成一个100长度数组,里面的数分别是1-100,并输出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class test {
     
        /**
         * @param args
         */
        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;
            }
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
            }
     
        }

     

      

     

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

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

     

      

     

     

     

posted @ 2021-04-19 20:21  兰佳淇  阅读(41)  评论(0)    收藏  举报