zoukankan      html  css  js  c++  java
  • java之day4补充

    传对象

    Student

    package day41;
    
    public class Student {
        int math;
        int chinese;
        int english;
    
        public Student(int math, int chinese, int english) {
            this.chinese = chinese;
            this.math = math;
            this.english = english;
        }
    }

    StudentCalc

    package day41;
    
    public class StudentCalc {
        double avg(Student student) {
            int chineseScore = student.chinese;
            int englishScore = student.english;
            int mathScore = student.math;
            int sum = student.chinese + student.math + student.english;
            return sum / 3;
        }
    }

    StudentTest

    package day41;
    
    public class StudentTest {
        public static void main(String[] args) {
            // 生成一个学生
            Student student = new Student(60, 70, 80);
            // 生成一个计算学生成绩的对象
            StudentCalc calc = new StudentCalc();
            double avg = calc.avg(student);
            System.out.println(avg);
        }
    }

    ========================================================================================================================

    Student

    package day42;
    
    public class Student {
    
        int height;
    
        public Student() {
        }
    
        public Student(int height) {
            this.height = height;
        }
    
    }

    StudentCalc

    package day42;
    
    public class StudentCalc {
        public double avg(Student[] students) {
            int sum = 0;
            for (int i = 0; i < students.length; i++) {
                sum += students[i].height;
            }
            return sum / 5;
        }
    
    }

    StudentTest

    package day42;
    
    import java.util.Scanner;
    
    public class StudentTest {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            Student[] student = new Student[5];
            for (int i = 0; i < 5; i++) {
                System.out.println("请输入第" + (i + 1) + "个学生身高:");
                int height1 = scanner.nextInt();
                student[i] = new Student();
                student[i].height = height1;
            }
    
            StudentCalc calc = new StudentCalc();
            double avg = calc.avg(student);
            System.out.println(avg);
    
        }
    
    }

  • 相关阅读:
    jQueryMobile(二)
    (六)JavaScript之[Regular Expression]与[错误(try, catch, throw)]
    18-metaclass,是潘多拉魔盒还是阿拉丁神灯?
    17-强大的装饰器
    15-Python对象的比较、拷贝
    13-搭建积木:Python模块化
    12-面向对象(下):如何实现一个搜索引擎?
    11-面向对象(上):从生活中的类比说起
    10-简约不简单的匿名函数
    09-不可或缺的自定义函数
  • 原文地址:https://www.cnblogs.com/haloxinghong/p/7406037.html
Copyright © 2011-2022 走看看