抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

JAVA 实验5.2

题目1

package IntArray;

import java.util.Scanner;

public class IntArray {
    int[] intArray = new int[10];

    public void intiElem() {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < intArray.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个数:");
            intArray[i] = sc.nextInt();
        }
    }

    public void printElem() {
        for (int i = 0; i < intArray.length; i++)
            System.out.printf("intArray[" + i + "]=" + intArray[i] + " ");
        System.out.printf("n");
    }

    public int maxElem() {
        int max = intArray[0];
        for (int i = 0; i < intArray.length; i++) {
            if (max < intArray[i]) {
                max = intArray[i];
            }
        }
        return max;
    }

    public int minElem() {
        int min = intArray[0];
        for (int i = 0; i < intArray.length; i++) {
            if (min > intArray[i]) {
                min = intArray[i];
            }
        }
        return min;
    }

    public int sumElem() {
        int sum = 0;
        for (int i = 0; i < intArray.length; i++) {
            sum += intArray[i];
        }
        return sum;
    }

    public double avgElem() {
        double sum = 0;
        for (int i = 0; i < intArray.length; i++) {
            sum += intArray[i];
        }
        return sum / intArray.length;
    }

    public static void main(String[] args) {
        IntArray s = new IntArray();
        s.intiElem();
        s.printElem();
        System.out.println("max:" + s.maxElem());
        System.out.println("min:" + s.minElem());
        System.out.println("sum:" + s.sumElem());
        System.out.println("avg:" + s.avgElem());
    }

}

题目2

package IntArray;

public class Student {
    private String name;
    private int id;
    private int grade;

    Student() {
    }

    Student(String name, int id, int grade) {
        this.id = id;
        this.name = name;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public int getGrade() {
        return grade;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Student[] s = new Student[5];
        s[0] = new Student("A", 12, 1);
        s[1] = new Student("B", 13, 2);
        s[2] = new Student("C", 14, 3);
        s[3] = new Student("D", 15, 4);
        s[4] = new Student("E", 16, 2);

        for (Student i : s) {
            if (i.getGrade() == 3) {
                System.out.println("该学生信息为:" + i.getName() + " " + i.getId() + " " + i.getGrade());
            }
        }
    }
}

本站由以下主机服务商提供服务支持:

0条评论

发表评论