第九章有参方法和包

有参方法和包

定义参数的方法

参数列表:
(数据类型 参数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);
    }
}


你可能感兴趣的:(第九章有参方法和包)