#JAVA# 学习笔记 if 和switch 语句

2018-06-23  本文已影响8人  LeeMin_Z

description:
This section describes the decision-making statements (if-then, if-then-else, switch), the looping statements (for, while, do-while), and the branching statements (break, continue, return) supported by the Java programming language.

1. if-else

跟C几乎一样,都是if - else if -else

  1. 可以只有if,后面两个可以省略。
  2. 注意判断条件只能是布尔类型的true/false。
  3. 只有if 的时候,等于是多个loop,每个loop单独判断。
  4. if - else if -else 时,从上往下判断,仅做第一个符合的条件,后续不再判断并跳出loop。

python的if判断没规定很死,int类型的1或0,还有其他也能当判断条件。JAVA规定很死,只能是布尔类型的true或false。

//sudo code 
if (true) then {
    perform actions ;
}
if (false) then {
    ignore this action ; 
}
//input
class BlockDemo {
    public static void main(String[] args) {
        boolean condition = true;
        if (condition) { // begin block 1
            System.out.println("Condition is true.");
        } // end block one
        else { // begin block 2
            System.out.println("Condition is false.");
        } // end block 2
    }
}

//output 
Condition is true.

//input 

class IfElseDemo {
    public static void main(String[] args) {

        int testscore = 76;
        char grade;

        if (testscore >= 90)
            grade = 'A';
        else if (testscore >= 80)
            grade = 'B';
        else if (testscore >= 70)
            grade = 'C';
        else if (testscore >= 60)
            grade = 'D';
        else
            grade = 'F';
        
        System.out.println("Grade = " + grade);
    }
}

//output 
Grade = C

#ipython 3.6, use number as the condition  

In [11]: yes = 20

In [12]: if yes:print("the condition is true")
the condition is true

In [13]: type(yes)
Out[13]: int

In [14]: yes = 0

In [18]: if yes:
    ...:     print("the condition is true")
    ...: else:
    ...:     print("the condition is false")
    ...:
the condition is false

2. The switch Statement

  1. 简单例子,switch意思就是跳到指定位置,需要有默认设置。
  2. 遇到break就跳出这个switch 圈。
//sudo code 

switch(case id/string){
case id 1 : actions 1;
case id 2: actions 2;
....
case id n: actions n;

break;  // jump out of switch condition {} 

}

  1. 只选一个case,需要在每个case后有break;指令
//input 
public class SwitchDemo {
    public static void main(String[] args) {

        int month = 8;
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                break;
            case 2:  monthString = "February";
                break;
            case 3:  monthString = "March";
                break;
            case 4:  monthString = "April";
                break;
            case 5:  monthString = "May";
                break;
            case 6:  monthString = "June";
                break;
            case 7:  monthString = "July";
                break;
            case 8:  monthString = "August";
                break;
            case 9:  monthString = "September";
                break;
            case 10: monthString = "October";
                break;
            case 11: monthString = "November";
                break;
            case 12: monthString = "December";
                break;
            default: monthString = "Invalid month";
                break;
        }
        System.out.println(monthString);
    }
}

//output 
August
  1. 仅跳到某行,且选择后续所有动作,break只需加在最后。(当然也可以看情况加)
//input 
public class SwitchDemoMultiple {

    public static void main(String[] args) {

        java.util.ArrayList<String> furtherMonth =
                new java.util.ArrayList<String>();

        int month = 2;

        switch (month) {
            case 1:
            furtherMonth.add("Jan");
            case 2:
            furtherMonth.add("feb");
            case 3:
            furtherMonth.add("march");
            case 4:
            furtherMonth.add("april");
            case 5:
            furtherMonth.add("may");
                break;
            default: break;
        }

        if (furtherMonth.isEmpty()) {
            System.out.println("no month further");
        } else {
                System.out.println(furtherMonth);
        }


    }

}

//output 
[feb, march, april, may]

  1. 可以多个case使用一样的actions;例如以下是判断某年某月有多少天。
class DaysInMonth {
    public static void main(String[] args) {

        int month = 2;
        int year = 1900;
        int DaysInMonth = 0;

        switch (month){
            case 1: case 3: case 5:
            case 7: case 8: case 10: case 12:
                DaysInMonth = 31;
                break;
            case 4: case 6: case 9 : case 11:
                DaysInMonth = 30;
                break;
            case 2:
                if((year%4 == 0 && year%100 != 0) || year % 400 == 0){DaysInMonth =29;}
                else{DaysInMonth = 28;}
                break;
             default:
                 System.out.println("invalid");
                 break;
        }
        System.out.println(DaysInMonth);
    }
}

  1. switch的判断条件可以是字符串。

例如这个程序需要分成两块,
a) 第一块是做判断,输入字符串的月份后;输出月份的阿拉伯数字
b) 第二是模块化中间的转换“字符串转换为阿拉伯数字月份”
c) 调用部分很像python中的使用某个库,如果C∈B∈A,调用方式就是A.B.C一层层找到C这样子。

class StringSwitchDemo {

    // trasation module

    public static int getMonthNumber(String month){
        int monthNumber = 0;
        if (month == null){return monthNumber;}
        else {
            switch (month.toLowerCase()){
                case "january":
                    monthNumber = 1;
                    break;
                case "february":
                    monthNumber = 2;
                    break;
            }
            return monthNumber;
        }
    }

    //input and print output

    public static void main(String[] args) {
        String month = "January";

        int returnMonthNumber = StringSwitchDemo.getMonthNumber(month);

        if (returnMonthNumber==0){
            System.out.println("invalid");
        } else {
            System.out.println(returnMonthNumber);
        }
    }
}

//test output 
1

例子依旧来自java8官方文档
2018.6.21

上一篇下一篇

猜你喜欢

热点阅读