第九章有参方法和包

有参方法和包

定义参数的方法

参数列表:
(数据类型 参数1,数据类型 参数2)

public class ZhazhiJi {
    public String zhazhi ( String fruit ) {
          String juice = fruit + "汁";
          return juice; 
     } 
}
调用参数的方法
ZhazhiJi myZhazhiji = new ZhazhiJi();
String myFruit = "苹果";
String myJuice = myZhazhi.zhazhi(myFruit);
System.out.println(myJuice);
image.png
定义带参数的方法

访问修饰符 返回类型 方法名( ) {
方法的主体
}
调用带参数的方法
对象名 . 方法名(参数1,参数2)

例:public class StudentsBiz {
    String[] names = new String[30];
    int index = 0;//记录数组中学员的个数,也就是下一个需要插入数组的下标位置
    public void addName(String name)
    {
        names[index] = name;
        index++;
    }
    public void showNames()
    {
        for(int i = 0; i < index; i++)
        {
            System.out.println(names[i]);
        }
    }
}


public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StudentsBiz studentsBiz = new StudentsBiz();
        for(int i = 0; i < 3; i++)
        {
            System.out.println("请输入姓名");
            String name = scanner.next();
            studentsBiz.addName(name);
        }
        studentsBiz.showNames();
    }
}
带多个参数的方法
public class StudentsBiz {
    String[] names = {"zhangsan","lisi","wangwu","liudehua"};

    public boolean searchName(String name,int start, int end)
    {
        if(end > names.length)
        {
            end = names.length;
        }
        if(start < 0)
        {
            start = 0;
        }

        for(int i = start; i < end; i++)
        {
            if(names[i].equals(name))
            {
                return true;
            }
        }

        return false;
    }
}
调用方法

public class Main {

    public static void main(String[] args) {
    // write your code here
//         zhangsan,lisi,wangwu,zhaobensan,liudehua
        StudentsBiz studentsBiz = new StudentsBiz();
        boolean b = studentsBiz.searchName("liudehua2",-5,8);
        System.out.println(b);
    }
}


数组作为参数
public class ScoreCalc {

    public int getTotalScore(int[] scores)
    {
        int totalScore = 0;
        for(int i = 0; i < scores.length; i++)
        {
            totalScore += scores[i];
        }
        return totalScore;
    }

    public double getAvgScore(int[] scores)
    {
        int totalScore = getTotalScore(scores);
        return (double) totalScore/scores.length;
    }

    public int getMaxScore(int[] scores)
    {
        int max = scores[0];//假设数组的第一个元素是最大
        for(int i = 1; i < scores.length; i++)
        {
            if(max < scores[i])
            {
                max = scores[i];
            }
        }

        return max;
    }
}
public class Main {

    public static void main(String[] args) {
    // write your code here
        ScoreCalc scoreCalc = new ScoreCalc();
        int[] arrays = {67,76,88,86,99};
        int total = scoreCalc.getTotalScore(arrays);
        System.out.println(total);
        double avg = scoreCalc.getAvgScore(arrays);
        System.out.println(avg);

        int max = scoreCalc.getMaxScore(arrays);
        System.out.println(max);
    }
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,806评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,798评论 1 32
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 7,893评论 2 9
  • 该文章内容来自易仁永澄老师《向组织揩油》活动 文章目的:将3月目标课程内容做梳理,整理老师讲课过程中的概念,观点,...
    夜蝴蝶FLY阅读 1,716评论 2 2
  • 生当作人杰,死亦为鬼雄。至今思项羽,不肯过江东。”这出自李清照《夏日绝句》的千古传诵的名句,高度赞扬了项羽这位英雄...
    胖字阅读 4,509评论 1 3