第七周上机+练习4.16

上机:

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

package week7;

public class P1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={10,20,30,40,50};
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }

    }

}

 

 


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

package week7;

import java.util.Scanner;

public class P2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        int a[]=new int[5];
        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 并在控制台输出

package week7;

public class P3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char a[]={'c','h','i','n','a'};
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }

    }

}

 

 


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

package week7;

import java.util.Scanner;

public class P4 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        double a[]=new double[5];
        double sum=0;
        for (int i = 0; i < a.length; i++) {
            a[i]=input.nextInt();
        }
        for (int i = 0; i < a.length; i++) {
            sum+=a[i];
        }
        System.out.println("总成绩为"+sum);
        System.out.println("平均成绩为"+sum/5);

    }

}

 

 


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

package week7;

public class P5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={12,53,23,44,53};
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }System.out.println("       ");
        for (int i = 4; i >0; i--) {
            System.out.println(a[i]);
        }System.out.println("      ");
        for(int i :a){
            System.out.println(i);
        }

    }

}

 

 


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

package week7;

public class W1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={23,54,43,76,23};
        int sum=0;
        int um=0;
        for (int i = 0; i < a.length; i++) {
            if(a[i]/2==0)
            {
                sum++;
            }
            else
            {
                um++;
            }
        }
        System.out.println(sum+"个偶数");
        System.out.println(um+"个奇数");

    }

}

 

 


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

package week7;

public class W2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]=new int[100];
        int b=0;
        for (int i = 0; i < a.length; i++) {
            b++;
            a[i]=b;
        System.out.println(a[i]);
        }
        
    }

}

 

 

 

 

 

 


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

package week7;

public class W3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double a[]={54,87,65,54,98,97,76,54,65,78};
        for (int i = 0; i < a.length; i++) {
            if(a[i]<=95&&a[i]>=0)
            {
                a[i]+=5;
            }
            else if(a[i]>95)
            {
                a[i]=a[i];
            }
            System.out.println(a[i]);
        }

    }

}

 

posted @ 2021-04-16 22:54  崔明月  阅读(41)  评论(0编辑  收藏  举报