方法详解

方法

方法是什么

在这里插入图片描述

package method;

public class Demo01 {
    public static void main(String[] args) {
        int sum = add(1, 2);//调用add方法,并把返回值赋值给sum
        System.out.println(sum); //输出sum
        text(); //调用text()方法
    }
    //编写一个方法,计算两个int参数的和,需要输出一个值,因此是int,写在外围
    
    public static int add(int a,int b){//定义两个参数的值
        return a+b;//返回参数的和的值
    }
    
    //编写一个方法,输出0-1000中的能被五整除的数,并三个一排输出
    public static void text(){ 
        int a =0;
        for (int i = 0; i <=1000; i++) {
            if (i%5==0){ //判断i能否被五整除
                System.out.print(i+"\t");//输出i的值和一个制表符
                a++;
                if (a==3){//判断a是否等于3
                    System.out.print("\n");//换行
            //也可   System.out.println();  
                    a=0;
                }
            }
        }
    }
}

疑难点

什么时候用void,什么时候不用void,如何快速正确的判断


方法的定义与调用

方法的定义

在这里插入图片描述

public class Demo01 {
    public static void main(String[] args) {
        int sum = add(1, 2); //1,2 实际参数
        System.out.println(sum);
    }
    
  //public  static修饰符   int方法返回值   add方法名字   a,b 形式参数
    public static int add(int a,int b){  
        return a+b;
    }   //整个add方法叫做方法体,它的功能是两个整数的加法

方法的调用

在这里插入图片描述

public class Demo02 {
    public static void main(String[] args) {
        int max = max(10, 10);
        System.out.println(max);
    }
    public static int max(int num1,int num2){
        int result=0;//初始化
        if (num1==num2){  
         System.out.println("num1=num2");
            return 0;  //终止方法
        }
        if (num1>num2){
            result=num1;
        }else{
            result=num2;
        }
        return result;
    }
}

输出结果

num1=num2
0

弊端:当num1=num2的时候还会输出0

方法很多

public class Text01 {
    public static void main(String[] args) {
        max(10,10);
    }
    public static void max(int num1,int num2){
        int result=0;//初始化
        if (num1==num2){
            System.out.println("num1=num2");
            return ;//终止方法
        }
        if (num1>num2){
            System.out.println(num1);
        }else{
            System.out.println(num2);
        }
    }
}

方法的重载

在这里插入图片描述

public static void main(String[] args) {
    int sum = add(1, 2);
    System.out.println(sum);
}
//加法  //所有的方法名称都是add
    public static int add(int a,int b){  //返回值为int,参数个数为2,参数名称a,b
    return a+b;
}
    public static double add(double a,double b){  //返回值类型为double
        return a+b;
    }
    public static int add(int a,int b,int c){  //参数个数为3
        return a+b+c;
    }
    public static int add(int aaa,int bb){   //形式参数不一样 aaa,bb
        return aaa+bb;
    }

命令行传递参数

了解一下,现在不用掌握

public class Demo03 {
    public static void main(String[] args) {
        for (int i=0;i< args.length;i++){   //args.length数组长度 
            System.out.println("args["+i+"]"+args[i]);
        }
    }
}

在这里插入图片描述

在这里插入图片描述


可变参数

在这里插入图片描述

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.test(1,2,3,4,5,6);
        printMax(34,3,3,2,56.5);
        printMax(new double[]{1,2,3});
    }
    public void test(int x,int y,int...i){ //方法中只有一个可变参数int...i,并且在任何普通参数的后面
    System.out.println(i[0])
    } 
    public static void printMax(double...numbers){
        if (numbers.length==0){
            System.out.println("No argument passed");
            return;
        }
        double result = numbers[0];

        //排序
        for (int i =1;i<numbers.length;i++){
            if (numbers[i]>result){
                result = numbers[i];
            }
        }
        System.out.println("The max value is"+result);
    }
}

在这里插入图片描述


递归

注:能不用递归就不用递归

注:能不用递归就不用递归

注:能不用递归就不用递归

在这里插入图片描述

public class Demo05 {
    public static void main(String[] args) {
        Demo05 test = new Demo05();
        test.test();
    }
    public void test(){
        test();
    }
}//我调用我自己  栈溢出异常

5的阶乘

public class Demo06 {
    public static void main(String[] args) {
        System.out.println(f(5)); //输出参数为5的方法f的返回值
    }
    //亦可写作
    //int result=f(5);  //将f(5)的值赋值给result
    //System.out.println(result); //输出result的值
    //代码越简单越好
    //代码越简单越好
    //代码越简单越好
    
    //2 2  *  f(1)
    //3 3 * f(2)
    public static int f(int n){  //f(5)= 5*f(4) = 5*4*f(3) = 5*4*3*f(2) = 5*4*3*2*1
        if (n==1){
            return 1;
        }else{
            return n*f(n-1);
        }
    }
}

小练

写一个计算器,要求实现加减乘除功能,并且能够循环接收新的数据,通过用户交互实现

  • 思路推荐
  • 写4个方法:加减乘除
  • 利用循环+switch进行用户交互
  • 传递需要的两个数
  • 输出结果
package method;

import java.util.Scanner;

public class Text01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        calc();
    }
    public static void calc() {
        for (int i = 1; i > 0; i++) {
            Scanner scanner = new Scanner(System.in);
            double a = scanner.nextDouble();
            String b = scanner.next();
            double c = scanner.nextDouble();
            String d = b;
            switch (d) {
                case "+":
                    System.out.println(a + "+" + c + "=" + (a + c));
                    break;
                case "-":
                    System.out.println(a + "-" + c + "=" + (a - c));
                    break;
                case "*":
                    System.out.println(a + "*" + c + "=" + (a * c));
                    break;
                case "/":
                    System.out.println(a + "/" + c + "=" + (a / c));
                    break;
            }
            i++;
        }
    }
}

缺点:如果输错,直接报错,结束运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值