Java基础之方法与流程控制

1.方法

定义一个方法的格式:

//方法名称的命名规则和变量一样,使用小驼峰
public static void 方法名称() {
    方法体
}
// 实例
public class Demo11Method {
    public static void main (String[] args) {
        famer();
        saler();
        cook();
        me();
        
    }
    public static void me() {
        //
        System.out.println("吃");
    }
    public static void cook() {
        // 小商贩
        // 厨子
        System.out.println("洗菜");
        System.out.println("切菜");
        System.out.println("炒菜");
        System.out.println("装盘");
    }
    public static void saler() {
        // 小商贩
        System.out.println("运输到农贸i市场");
        System.out.println("抬高价格");
        System.out.println("卖给厨子");
    }
    public static void famer() {
        // 农民
        System.out.println("播种");
        System.out.println("浇水");
        System.out.println("施肥");
        System.out.println("除虫");
        System.out.println("收割");
        System.out.println("卖给小商贩");
    }
}
View Code

注意事项:

  • 方法定义的先后顺序无所谓

  • 方法的定义不能产生嵌套包含关系

  • 方法定义好了之后不会执行,需要进行调用

补充注意:

对于byte/short/char三种类型来说,如果右侧赋值的数值没有超过范围,那么javac编译器将会自动隐含的为我们补上一个(byte)(short)(char)

  • 如果没有超过左侧的范围,编译器补上强转

  • 如果右侧超过了范围,那么直接编译报错

再给变量赋值的时候,如果右侧的表达式当中全部都是常量的时候,没有任何的变量,那么编译器javac将会直接将若干个常量表达式计算得到结果。但是注意:如果表达式中有变量参与,那么就不能进行这种优化了

方法其他注意事项:

1.方法应该定义在类当中,但是不能定义在方法中,不能嵌套

2.方法定义前后的顺序无所谓

3.方法定义之后不会执行,如果希望执行,需要调用:单独调用、打印调用、赋值调用

4.如果方法有返回值,那么必须写上:return 返回值,不能没有

5.return后面的返回值数据,必须和方法的返回值类型对应起来

6.对于一个void没有返回值的方法,不能写return后面的返回值,只能写return自己

7.对于方法中的最后一行的return可以省略不写

8.一个方法当中可以有多个return语句,但是必须保证同时只有一个会被执行到

方法重载

方法重载(overload):多个方法的名称一样,但是参数列表不能一样

优点:只需要记住一个唯一的方法名称,就可以实现类似的多个功能

方法重载与下列因素相关:

  1. 参数个数不同

  2. 参数类型不同

  3. 参数的多类型顺序不同

方法重载与下列方法无关:

  1. 不能根据参数的名字进行重载,与参数的名称无关

  2. 与方法的返回值无关

package com.dcits.day02.demo04;
​
public class Demo01MethodOverload {
    public static void main(String[] args) {
        System.out.println(sum(1,3,4));
    }
    public static int sum(int a,int b) {
        System.out.println("有两个参数的方法执行");
        return a + b;
    }
    public static int sum(int a,int b,int c) {
        System.out.println("有三个参数的方法执行");
        return a + b + c;
    }
    public static int sum(int a,int b,int c,int d) {
        System.out.println("有四个参数的方法执行");
        return a + b +c + d;
    }
}

2.流程控制

2.1 判断语句

单if语句

public class Demo02If {
    public static void main (String[] args) {
        System.out.println("今天天气不错,去网吧");
        int age = 16;
        if (age >= 18) {
            System.out.println("LOL");
        }
        System.out.println("回家吃饭");
    }
}

标准的if——else语句

public class Demo02If {
    public static void main (String[] args) {
        int age = 16;
        if (age % 2 == 0) {
            System.out.println("偶数");
        } else {
            System.out.println("奇数");
        }
        
    }
}

if—else,if—else

public class Demo02If {
    public static void main (String[] args) {
        int x = 10;
        int y ;
        if (x >= 3) {
            y = 2*x + 1;
        }else if (-1 < x && x < 3) {
            y = 2 * x;
        } else {
            y = 2*x - 1;
        }
        System.out.println("结果是" + y);
    }
}

使用三元运算符和标准的if-else语句分别实现:去两个数中的最大值

// 三元运算
public class Demo02If {
    public static void main (String[] args) {
        int a = 10;
        int b = 20;
        int result =  a > b ? a : b;
        System.out.println(result);     
    }
}
​
// if表达式
public class Demo02If {
    public static void main (String[] args) {
        int a = 10;
        int b = 20;
        int result;
        if (a > b) {
            result = a;
        }else {
            result = b;
        }
        System.out.println(result);
    }
}

switch语句

多个case后面的数值不能重复

switch后面小括号中只能是以下数据类型:

  • 基本数据类型:byte、short、char、int

  • 引用数据类型:String字符串、enum枚举

switch语句可以很灵活:前后顺序可以颠倒,而且break语句也可以省略。匹配哪一个case就从哪一个位置向下执行,直到遇到了break会哦这整体结束为止

public class Demo02If {
    public static void main (String[] args) {
        int num = 5;
        switch (num) {
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期日");
                break;
            default:
                System.out.println("数据不合理");
                break;
        }
    }
}

2.2 循环语句

for循环

for (初始化表达式; 布尔表达式; 步进表达式) {
    循环体
}
​
public class Demo09For {
    public static void main (String[] args) {
        for (int i = 1;i<=100;i++) {
            System.out.println("我错啦!" + i);
        }
        System.out.println("程序停止");
    }
}

while循环

public class Demo09For {
    public static void main (String[] args) {
        int i = 1;
        while (i <= 10) {
            System.out.println("我错啦" + i);
            i++;
        }
        System.out.println("程序停止");
    }
}

do..while循环

public class Demo09For {
    public static void main (String[] args) {
        int i = 1;
        do {
            System.out.println("我错啦" + i);
            i ++;
        } while (i<=10);
        System.out.println("程序停止");
    }
}

三种循环的区别

1.如果条件判断从来就没有满足过,那么for循环和while循环将会执行0次,但是do while循环至少会执行一次

2.for循环的变量在小括号中定义,只有在循环内部才可以使用

2.3 条件控制语句

break

1.可以在switch语句中,一旦执行,整个switch语句立刻结束

2.还可以用在循环语句当中,一旦执行,整个循环语句立刻结束,打断循环

public class Demo09For {
    public static void main (String[] args) {
        int sum = 0;
        for (int i = 1;i <=100; i++) {
            if (i  == 10) {
                break;      
            }
        }
        System.out.println(sum);
    }
}

continue

public class Demo09For {
    public static void main (String[] args) {
        int sum = 0;
        for (int i = 1;i <=10; i++) {
            if (i  == 4) {
                continue;       
            }
            System.out.println(i + "层到了");  
        }
    }
}

死循环

public class Demo09For {
    public static void main (String[] args) {
        while (true) {
            System.out.println("I love Java!");
        }
    }
}

循环的嵌套

public class Demo09For {
    public static void main (String[] args) {
        for (int hour = 0; hour < 24; hour++) {
            
            for (int minute = 0;minute <60; minute ++){
                System.out.println(hour + "点" + minute + "分");
            }
        }   
    }
}
原文地址:https://www.cnblogs.com/guoruijie/p/13294629.html