方法/流程控制

方法:

命名的代码块

方便重复调用

减少重复代码

public class Test1 {

    public static void main(String[] args) {

        System.out.println(1);

        f();

        System.out.println(2);

        f();

        System.out.println(3);

        f();

    }

 

    private static void f() {

        System.out.println(4);

        System.out.println(5);

    }

}

方法的参数

Static void f(int a){}

f(3)

 

package day0301;

 

public class Test2 {

    public static void main(String[] args) {

        System.out.println(1);

        f(2);

        System.out.println(3);

        f(4);

        System.out.println(5);

        f(6);

    }

 

    private static void f(int i) {

        System.out.println(i*i);

    }

}

 

流程控制

If

Switch

For

While

Do…while

 

If

If(条件){代码}

package day0302;

 

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        /**

         * 输入号码:

         * 以4结尾 免费

         * 8结尾 80

         * 其他 30

         */

        System.out.print("请输入号码:");

        String tel = new Scanner(System.in).nextLine();

        f(tel);

    }

 

    private static void f(String tel) {

       

        if(tel.length() != 11){

            System.out.println("号码不正确");

            return;

        }

       

        if(tel.endsWith("4"))

        {

            System.out.println("免费");

        }else if(tel.endsWith("8")){

            System.out.println(80);

        } else {

            System.out.println(30);

        }

    }

}

个人所得税

package day0303;

 

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.print("输入税前工资:");

        double v = new Scanner(System.in).nextDouble();

        f(v);

    }

 

    private static void f(double v) {

        if (v <= 5000) {

            System.out.println("不用交税");

            return;

        }

 

        v -= 5000;

        double r;

        int k;

 

        if (v <= 3000) {

            r = 0.03;

            k = 0;

        } else if (v > 3000 && v <= 12000) {

            r = 0.1;

            k = 210;

        } else if (v > 12000 && v <= 25000) {

            r = 0.2;

            k = 1410;

        } else if (v > 25000 && v <= 35000) {

            r = 0.25;

            k = 2660;

        } else if (v > 35000 && v <= 55000) {

            r = 0.3;

            k = 4410;

        } else if (v > 55000 && v <= 80000) {

            r = 0.35;

            k = 7160;

        } else {

            r = 0.45;

            k = 15160;

        }

        System.out.println("应交税:" + (v * r - k));

    }

}