• Java中的带参方法


    1.有返回值的带参方法

    看下面代码:

    1)字符串型:

    	public String  zhaZhi( String shuiGuo){   
    		return  shuiGuo+"汁";
    

     代码括号中的为参数,String为返回值类型,所以返回一个字符串类型;

    2)数组作为返回值:

    把原数组中的每个元素都扩大2倍:

    public class Tool{
            public  int[]  change(int [] num){
                 for (int i=0;i<=num.length;i++)
                       num[i]=num[i]*2
        }
    
                      return num;//返回数值
    }
        public class MyMain{
            public static void main(String[] args){
                        Tool tool=new Tool();  //创建对象
                       int[] num={1,2,3}
       System.out.print(“改变前的数组”)
                      for (int i=0;i<=num.length;i++){
                       System.out.print( num[i]) 
          }
            
           int[]  newNum=tool. change( num)
                 System.out.print(“改变后的数组”)
             for (int i=0;i<=num.length;i++){
                       System.out.print( num[i]) 
          }
    }
    
    }
    
    2.无返回值的带参方法
    1)往学员数组中插入一个新的名字,并查找是否存在

    public class StudentBiz {
    String [] names=new String[3];
    public void addStuName(String name){
    for (int i = 0; i <names.length; i++) {
    if (names[i]==null) {
    names[i]=name;
    break;
    }
    }
    }

    public boolean findStu(String name){
    boolean flag=false;
    for (int i = 0; i < names.length; i++) {
    if (names[i]!=null) {
    if (names[i].equals(name)) {
    flag=true;
    break;
    }
    }
    }
    return flag;
    }

    public void print(){
    for (int i = 0; i < names.length; i++) {
    System.out.println(names[i]);
    }
    }
    }

        

    public class TestStudentBiz {
    	public static void main(String[] args) {	
    	StudentBiz biz=new StudentBiz();		  
    	 biz.addStuName("白百合");
    	 biz.print();
    	
    	 StudentBiz p1=new StudentBiz();
    	 boolean flag = p1.findStu("张三");
    	 
    	if (flag) {
    		System.out.println("yes");
    	}else{
    		System.out.println("no");
    	}
    

      

      

  • 相关阅读:
    美国地质调研局USGS
    SAR 图像
    Matlab 之meshgrid, interp, griddata 用法和实例
    ENVISAT卫星及ASAR数据介绍
    ASP.NET Integration with IIS7
    ubuntu下C/C++基本开发环境的配置
    C++ Objects Part 1: Basic Object Memory Layout
    Socket Programming in Windows
    Memory Layout for Multiple and Virtual Inheritance
    Common Type System—Memory Layout at C# Online.NET
  • 原文地址:https://www.cnblogs.com/cn-930621/p/6724263.html
Copyright © 2020-2023  润新知