带参方法与返回值的几个例子

1.带一个参数的方法

 

 1 package cn.happy3;
 2 
 3 public class MyMain {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         //1.购买一台榨汁机
10         ZhaZhiJi machine=new ZhaZhiJi();
11         
12         //2.购买水果
13         String xxx="苹果";
14         
15         //3.bind过程  榨汁
16         String result = machine.zhazhi(xxx);
17         System.out.println(result);
18 
19     }
20 }
 1 package cn.happy3;
 2 /**
 3  * 榨汁机类
 4  * @author Happy
 5  *
 6  */
 7 public class ZhaZhiJi {
 8    //只关注我需要,,,设计理念
 9     //榨汁的方法
10     public String zhazhi(String fruit){//水果
11         fruit=fruit+"被榨成果汁";
12         return fruit;
13     }
14 }

2.带多个参数的方法

 1 package cn.happy4;
 2 /**
 3  * 计算类
 4  * @author Happy
 5  *
 6  */
 7 public class Calculator {
 8     //3.接收用户传入的等待计算的数字
 9    //方法,计算 两个数的和
10     public int add(int numLeft,int numRight){
11         //4.加工运算
12         int sum=numLeft+numRight;
13         //5.返回结果
14         return sum;
15     }
16 }
 1 package cn.happy4;
 2 
 3 public class MyMain {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         //1.购买一个计算器,就是为了后续使用它提供的计算功能
10         Calculator cal=new Calculator();
11         int num1=1;
12         
13         //6.接收方法调用的结果
14         int result=
15                 //2.准备两个操作数,作为方法参数传入
16                 cal.add(num1, 1);
17         //7.打印结果
18         System.out.println(result);
19         
20         int result2=cal.add(99, 99);
21         
22         System.out.println(result2);
23 
24     }
25 
26 }

3、数组作为参数示例

 1 package cn.happy6;
 2 
 3 public class Tool {
 4     //改变值  将传入的数组加工
 5    public int[] changeValue(int[] nums){
 6        //变成原来的两倍
 7        for (int i = 0; i < nums.length; i++) {
 8            nums[i]=nums[i]*2;
 9        }
10        return nums;
11    }
12    
13 }
 1 package cn.happy6;
 2 
 3 public class MyMain {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9          Tool tool=new Tool();
10          
11          int[] nums={1,2,3};
12          System.out.println("改变前==================");
13          for (int i = 0; i < nums.length; i++) {
14             System.out.println(nums[i]);
15          }
16 
17          int[] newNums = tool.changeValue(nums);
18          
19          System.out.println("after change==================");
20          for (int i = 0; i < newNums.length; i++) {
21             System.out.println(nums[i]);
22          }
23     }
24 
25 }

4.

有一个长度为3的数组,类型是String[]
1)提供一个类,定制一个addStuName() 能帮我向数组中添加元素
StudentBiz Business业务

2)请你定制一个方法,查找”凤姐“ 有没有在数组中。

 1 package cn.happy5;
 2 /**
 3  * 学生管理类
 4  * @author Happy
 5  *
 6  */
 7 public class StudentBiz {
 8    public String[] names=new String[3];   //__null_   null     null__
 9    /**
10     * 
11     * @param name  要查找的姓名
12     * @param startPos   查找的起始位置   1
13     * @param endPos     查找的结束位置   3
14     */
15    public boolean  findGDS(String name,int startPos,int endPos){
16        boolean flag=false;//查找失败
17        for (int i = startPos-1; i <=endPos-1; i++) {
18            //金牌结论:如果一个对象为null,那么不能使用它的任何属性和方法
19            if (name.equals(names[i])) {
20                flag=true;
21                break;
22            }
23        }
24        return flag;
25    }
26  
27    //查找某个姓名是否在names中
28    public boolean findName(String name){
29        boolean flag=false;
30        
31        for (int i = 0; i < names.length; i++) {
32            if (names[i]!=null) {
33               if(names[i].equals(name)){
34                   flag=true;
35                   break;
36               }
37            }
38        }
39        return flag;
40        
41    }
42    
43    //功能 添加
44    public void addStuName(String name){
45        //for
46        for (int i = 0; i < names.length; i++) {
47           if (names[i]==null) {  //这个就是要添加的位置
48               names[i]=name;
49               break;
50           }   
51        }
52    }   
53    //打印数组内容
54    public void printStudents(){
55        for (int i = 0; i < names.length; i++) {
56           System.out.println(names[i]);
57        }
58    }
59 }
 1 package cn.happy5;
 2 
 3 import java.util.Scanner;
 4 
 5 public class MyMain {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         
12         StudentBiz biz=new StudentBiz();
13         boolean flag = biz.findGDS("张三",1,3);
14         if (flag==true) {
15             System.out.println("找到了");
16         }else {
17             System.out.println("sorry,can't find ");
18         }

 

posted @ 2017-04-15 13:50  斜月阑珊  阅读(682)  评论(0编辑  收藏  举报