方法的创建与使用

方法基础用法
public class Demo01 {
    //main方法
    public static void main(String[] args) {
        //调用方法
        int add = add(3,4);
        System.out.println(add);
        test();
    }
    public static int add(int a, int b){
        //实现两个数相加
        return a+b;
    }

    public static void test(){
        int count = 0;
        for (int i =0;i <= 1000;i++){
            if (i % 5 ==0){
                System.out.print(i + "\t");
                count++;
                if (count % 3 ==0){
                    System.out.println();
                }
            }
        }
    }

}

public class Demo02 {
    public static void main(String[] args) {
        double max = max(10.0,20.0);
        int max01 = max(10,20);
        System.out.println(max);
        System.out.println(max01);
    }
    //比大小
    public static int max(int a,int b){
        if(a>b){
            return a;
        }else if (a<b){
            return b;
        }else {
            System.out.println("两个数大小相等");
            return 0;
        }
    }
    //方法的重载,方法名一致,参数不同
    public static double max(double a,double b){
        if(a>b){
            return a;
        }else if (a<b){
            return b;
        }else {
            System.out.println("两个数大小相等");
            return 0;
        }
    }
}
public class Demo03 {
    public static void main(String[] args) {

        Demo03 demo03 = new Demo03();
        demo03.test(1,23,4,5);
    }
    //可变参数 一个方法中只能有一个可变参数,必须是方法的最后一个参数,任何普通的参数必须在他之前声明
    public void test(int...i){
        System.out.println(i[0]);
        System.out.println(i[1]);
        System.out.println(i[2]);
        System.out.println(i[3]);
    }

}
public class Demo04 {
    public static void main(String[] args) {
        int b = f(4);
        System.out.println(b);

    }
   //阶乘
    public static int f(int a){
        if(a==1 || a==0) {
            return 1;
        }
        else {
            return a*f(a-1);
        }
    }

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        //简单加减乘除计算器的实现
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要进行运算的两个整数:");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        System.out.println("请输入要进行运算的符号(加减乘除):");
        String src = scanner.next();

        switch (src){
            case "+":
                System.out.println(Add(a,b));
                break;
            case "-":
                System.out.println(Dec(a,b));
                break;
            case "*":
                System.out.println(Ride(a,b));
                break;
            case "/":
                System.out.println(Div(a,b));
                break;
            default:
                System.out.println("输入的符号错误");
        }

    }
    public static double Add(double a,double b){
        return a+b;
    }
    public static double Dec(double a,double b){
        return a-b;
    }
    public static double Ride(double a,double b){
        return a*b;
    }
    public static double Div(double a,double b){
        if(b!=0) {
            return a/b;
        }else {
            System.out.println("输入错误");
            return -1;
        }
    }
}

posted @ 2025-01-14 16:58  EndeavorX  阅读(27)  评论(0)    收藏  举报