วันจันทร์ที่ 2 พฤศจิกายน พ.ศ. 2558

Lab 8 - (Java) Find/count number of students with age < 30


public class Student {

    private String name;
    private int id;
    private int age;
    private int weight;
    private int height;

    public Student(String name, int id, int age, int weight, int height) {

        this.name = name;
        this.id = id;
        this.age = age;
        this.weight = weight;
        this.height = height;

    }

    public void display() {
        System.out.println(this.name);
        System.out.println(this.id);
        System.out.println(this.age);
        System.out.println(this.weight);
        System.out.println(this.height);

    }

    public int get_age() {
        return this.age;

    }

    public static void main(String[] args) {

        Student[] info = {new Student("Bun Tun", 00001, 18, 68, 149),
            new Student("FlukeKnub", 00002, 17, 50, 179),
            new Student("BasBomba", 00003, 20, 44, 188),
            new Student("GaseKiki", 00004, 21, 67, 180),
            new Student("PeeJa", 00005, 15, 88, 155)};

        age_average(info);

    }

    public static void age_average(Student[] e) {

        int count = 0;
        int i = 0;

        while (i < e.length) {

            if (e[i].get_age() < 30) {
                e[i].display();
                System.out.println();
                count = count + 1;
            }
            i = i + 1;
        }

        System.out.println(count);
    }

}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น