• Java方法


    System.out.printIn()

    类.方法.对象

    public static void main()

    public 修饰符

    static 类变量

    void 返回值类型 void代表不返回东西

    main 方法名

    package com.xiaojie.method;
    
    public class Demo01 {
        public static void main(String[] args) {
            int sum = add(1, 2);
            System.out.println(sum);
    
            System.out.println("=============================");
            test();
        }
    
        //加法
        public static int add(int a,int b) {
            return a+b;
        }
    
        public static void test(){
            int i = 0;
            while (i<=1000) {
                i++;
                if (i%5==0) {
                    System.out.print(i+"	");
                }
                if (i%(5*3)==0) {
                    System.out.print("
    ");
                }
            }
        }
    }
    

    package com.xiaojie.method;
    
    public class Demo01 {
        public static void main(String[] args) {
    
            //实际参数:实际调用传递给他的参数
            int sum = add(1, 2);
            System.out.println(sum);
        }
    
        //加法
        //形式参数,用来定义作用的
        public static int add(int a,int b) {
            return a+b;
        }
    }
    

    package com.xiaojie.method;
    
    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("两次输入的数值相等");
                return 0; //终止方法,类似while中的break
            }else if (num1 > num2) {
                result = num1;
            }else {
                result = num2;
            }
    
            return result;
        }
    }
    

    package com.xiaojie.method;
    
    public class Demo02 {
        public static void main(String[] args) {
            int max = max(20,10,30);
            System.out.println(max);
        }
    
        //比大小
        public static int max(int num1,int num2) {
    
            int result = 0;
            if (num1==num2) {
                System.out.println("两次输入的数值相等");
                return 0; //终止方法,类似while中的break
            }else if (num1 > num2) {
                result = num1;
            }else {
                result = num2;
            }
    
            return result;
        }
        //比大小
        public static double max(double num1,double num2) {
    
            double result = 0;
            if (num1==num2) {
                System.out.println("两次输入的数值相等");
                return 0; //终止方法,类似while中的break
            }else if (num1 > num2) {
                result = num1;
            }else {
                result = num2;
            }
    
            return result;
        }
        //比大小
        public static int max(int num1,int num2,int num3) {
    
            int result = 0;
            if (num1==num2) {
                System.out.println("两次输入的数值相等");
                return 0; //终止方法,类似while中的break
            }else if (num1 > num2) {
                result = num1;
            }else {
                result = num2;
            }
    
            return num3;
        }
    }
    

    package com.xiaojie.method;
    
    public class Demo03 {
        public static void main(String[] args) {
            //args.length 数组长度
            for (int i = 0; i < args.length; i++) {
                System.out.println("args["+i+"]: "+args[i]);
            }
        }
    }
    
    D:JavaSE基础语法srccomxiaojiemethod>javac Demo03.java
    
    D:JavaSE基础语法srccomxiaojiemethod>java Demo03
    错误: 找不到或无法加载主类 Demo03
    
    D:JavaSE基础语法srccomxiaojiemethod>cd ../../../
    
    D:JavaSE基础语法src>java com.xiaojie.method.Demo03
    
    D:JavaSE基础语法src>java com.xiaojie.method.Demo03 Hello World !
    args[0]: Hello
    args[1]: World
    args[2]: !
    

    package com.xiaojie.method;
    
    public class Demo04 {
        public static void main(String[] args) {
            Demo04 demo04 = new Demo04();
            demo04.printMax(10,40,20,50,100,23);
            demo04.printMax(new double[] {1,2,4,5,3});
        }
        public void printMax (double... numbers) {
            if (numbers.length == 0) {
                System.out.println("输入有误请重试");
                return;
            }
            double result = numbers[0];
            for (int x = 1; x < numbers.length; x++) {
                if (numbers[x] > result) {
                    result  = numbers[x];
                }
            }
            System.out.println(result);
        }
    }
    

    边界条件:边界

    前阶段:当参数不等于1时,不停调用自身直到等于1的时候,返回了结果

    放回阶段:从最大的数开始 n*(n-1)

    Java属于栈机制

    package com.xiaojie.method;
    
    public class Demo05 {
        //阶乘
        //2!    2*1
        //3!    3*2*1
        //4!    4*3*2*1
        public static void main(String[] args) {
            System.out.println(f(5));
        }
    
        public static int f(int n){
            if (n == 1) {
                return 1;
            }else {
                return n*f(n-1);
            }
        }
    
    }
    

    字符交互简易计算器

    package com.xiaojie.method;
    
    import java.util.Scanner;
    
    public class Demo07 {
        /*
    计算机
    接收用户的数据,写四个方法负责加减乘除
    利用循环和switch进行交互
     */
        public static void main(String[] args) {
            System.out.println("输入要计算的格式");
            Scanner scanner = new Scanner(System.in);
            String s = scanner.next();
            switch (s){
                case "+" :
                    Demo07 add = new Demo07();
                    add.addition();
                    break;
                case "-" :
                    Demo07 sub = new Demo07();
                    sub.subtraction();
                    break;
                case "*" :
                    Demo07 mul = new Demo07();
                    mul.multiply();
                    break;
                case "/" :
                    Demo07 div = new Demo07();
                    div.division();
                    break;
                default :
                    System.out.println("请选择 + - * / ");
            }
    
    
        }
        public double addition() {
            double num = 0;
            int x = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入数字");
            while (scanner.hasNextDouble()) {
                double i = scanner.nextDouble();
                num += i;
                x++;
                System.out.println("当前输入第"+x+"次,本次输入的数字是"+i);
            }
            System.out.println("合计:"+num);
            scanner.close();
            return 0;
        }
        public double subtraction() {
            double num = 0;
            int x = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入数字");
            while (scanner.hasNextDouble()) {
                double i = scanner.nextDouble();
                num -= i;
                x++;
                System.out.println("当前输入第"+x+"次,本次输入的数字是"+i);
            }
            System.out.println("合计:"+num);
            scanner.close();
            return 0;
        }
        public double multiply() {
            double num = 1;
            int x = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入数字");
            while (scanner.hasNextDouble()) {
                double i = scanner.nextDouble();
                num *= i;
                x++;
                System.out.println("当前输入第"+x+"次,本次输入的数字是"+i);
            }
            System.out.println("合计:"+num);
            scanner.close();
            return 0;
        }
        public double division() {
            double num = 0.0;
            int x = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入数字");
            while (scanner.hasNextDouble()) {
                double i = scanner.nextDouble();
                if (num == 0.0) {
                    num = i;
                    x++;
                    System.out.println("当前输入第"+x+"次,本次输入的数字是"+i);
                    continue;
                }
                num /= i;
                x++;
                System.out.println("当前输入第"+x+"次,本次输入的数字是"+i);
            }
            System.out.println("合计:"+num);
            scanner.close();
            return 0;
        }
    }
    
  • 相关阅读:
    LCA问题的离线处理Tarjan算法模版
    匈牙利算法 模版
    poj 1190 dfs
    poj 1376 bfs
    划分树模版
    让innerHTML的脚本也可以运行起来
    Keycode对照表
    Javascript 操作XML简单介绍
    Webdings和Wingdings字符码对应表
    动态加载JS脚本的4种方法
  • 原文地址:https://www.cnblogs.com/Notesdata/p/14077471.html
Copyright © 2020-2023  润新知