• 小白的java学习之路 “ 带参数的方法”



    一.带参数的方法:

                    1.1 语法:
                                <访问修饰符>  返回类型  <方法名>(<形式参数列表>) {
    
                                          //方法的主体
                                }
    1.2 案例:
                                榨汁机
                                public class ZhazhiJi {
                                    //带参数的方法
                                    public String zhazhi(String fruit){//形参
                                        String juice=fruit+"";
                                        return juice;
                                    }
                                    
                                    
                                }
                                public class ZhazhijiTest {
                                    public static void main(String[] args) {
                                        //创建类的对象实例
                                        ZhazhiJi zhazhiji=new ZhazhiJi();
                                        //用.的方式进行调用
                                        String fruit="苹果";
                                        String juice = zhazhiji.zhazhi(fruit);    //实参
                                        System.out.println(juice);
                                    }
                                }

    1.3 形参和实参:
    形参代表定义方法时括号内的参数(参数列表)       public void xc(形参)
    实参代表在调用方法时方法括号内的参数(参数列表) .    xc(实参);
    1.4 携带多个参数:

    注意:当携带多个参数时,实参列表需要和形参列表的顺序和数据类型保持一致

                                    案例:从开始位置开始查找数组中是否有学生姓名,查找到结束位置
                                            //从开始位置到结束位置查找学生
                                            public void seachStudent(int firstindex,int secondindex,String name){
                                                for (int i = firstindex; i <=secondindex; i++) {
                                                    if(names[i].equals(name)){
                                                        System.out.println("找到了!");
                                                        break;
                                                    }else{
                                                        System.out.println("没找到!");
                                                    }
                                                }
                                            }
    
                                            stu.seachStudent(2,8,"陈八");
    二.数组作为参数:
                            案例:有5位学员参加了Java知识竞赛的决赛,输出决赛的平均成绩和最高成绩
                                public class JavaScore {
        
                                    
                                    //计算平均成绩
                                    public double avgscore(double[] score){
                                        double avg=0.0;        //平均分
                                        double sum=0.0;        //总成绩
                                        for (int i = 0; i < score.length; i++) {
                                            sum=sum+score[i];
                                        }
                                        avg=sum/score.length;
                                        return avg;
                                    }
                                    
                                    //计算数组的最高成绩
                                    public double maxscore(double[] score){
                                        double max=score[0];        //最高分
                                        for (int i = 0; i < score.length; i++) {
                                            if(max<score[i]){
                                                max=score[i];
                                            }
                                        }
                                        return max;
                                    }
                                }
                                public class JavaScoreTest {
                                    public static void main(String[] args) {
                                        double [] scores=new double[5];
                                        Scanner input=new Scanner(System.in);
                                        for (int i = 0; i < scores.length; i++) {
                                            System.out.println("请输入第"+(i+1)+"位学员的成绩:");
                                            double score=input.nextDouble();
                                            scores[i]=score;
                                        }
                                        JavaScore javascore=new JavaScore();
                                        double avgscore = javascore.avgscore(scores);
                                        double maxscore = javascore.maxscore(scores);
                                        System.out.println("平均成绩为:"+avgscore);
                                        System.out.println("最高成绩为:"+maxscore);
                                    }
                                }

    三.对象作为参数:
    对象数组:
    例如:Student [] stus=new Student[5];
    访问对象数组中的元素通过stus[]的方式进行访问

    案例:在实现了增加一个学生姓名的基础上,增加学生的学号、年龄和成绩,并显示这些信息
                                public class Student {
    
                                    int stuno; // 学号
                                    String name; // 姓名
                                    int age; // 年龄
                                    double score; // 成绩
                                    /**
                                     * 创建一个数组:保存学
    
    生
                                     */
                                    Student[] stus = new Student[30]; // 对象数组:存储一堆对象 对象类型
    
                                    // 添加学生
                                    public void addStudent(Student stu) {
                                        for (int i = 0; i < stus.length; i++) {
                                            if (stus[i] == null) {
                                                stus[i] = stu;
                                                break;
                                            }
                                        }
                                    }
    
                                    // 展示学生信息
                                    public void showStudent() {
                                        for (int i = 0; i < stus.length; i++) {
                                            if (stus[i] != null) {
                                                System.out.println(stus[i].stuno + "	" + stus[i].name + "	"
                                                        + stus[i].age + "	" + stus[i].score);
                                            }
                                        }
                                    }
    
                                }
                                public class StudentTest {
                                    public static void main(String[] args) {
                                        /*Student stu1=new Student();
                                        stu1.name="陈璟瑜";
                                        stu1.stuno=001;
                                        stu1.age=20;
                                        stu1.score=99.99;
                                        stu1.addStudent(stu1);
                                        Student stu2=new Student();
                                        stu2.name="杨金川";
                                        stu2.stuno=002;
                                        stu2.age=18;
                                        stu2.score=100;
                                        stu1.addStudent(stu2);
                                        stu1.showStudent();*/
                                        
    
    
                                        Scanner input=new Scanner(System.in);
                                        Student stus=new Student();
                                        for (int i = 1; i <=5; i++) {
                                            Student stu=new Student();
                                            System.out.println("请输入第"+i+"位学生编号:");
                                            stu.stuno=input.nextInt();
                                            System.out.println("请输入第"+i+"位学生姓名:");
                                            stu.name=input.next();
                                            System.out.println("请输入第"+i+"位学生年龄:");
                                            stu.age=input.nextInt();
                                            System.out.println("请输入第"+i+"位学生成绩:");
                                            stu.score=input.nextDouble();
                                            stus.addStudent(stu);
                                        }
                                        stus.showStudent();
                                        
                                        
                                    } 
                                }


    四.包
    好处:

    1.允许类组成较小的单元(类似文件夹),易于找到和使用相应的文件
    2.防止命名冲突
    3.更好的保护类、属性和方法
    创建包的语法:package
    导入包的语法:import


    创建包的两种方式:
    1.选择项目下的src目录右键-->New-->package
    2.创建类的时候指定包名,如果没有包,则为默认的default package

  • 相关阅读:
    POJ 1287 Networking
    2019 牛客多校第六场 D Move
    2019 牛客多校第六场 J Upgrading Technology
    2019 牛客多校第六场 B Shorten IPv6 Address
    POJ 1251 Jungle Roads
    POJ 3268 Silver Cow Party
    POJ 3259 Wormholes
    Codeforces Round #596 (Div. 1, based on Technocup 2020 Elimination Round 2)
    Educational Codeforces Round 75 (Rated for Div. 2)
    【知识点】多项式乘法
  • 原文地址:https://www.cnblogs.com/wishsaber/p/9106147.html
Copyright © 2020-2023  润新知