1.if(){}else{}分支
代碼如下:
public class ifDome { public static void main(String[] arys){ int x = 10; if (x<30){ System.out.print("你個窮鬼,這是if語句,學會了嗎?"); }else { System.out.print("着是else語句!你的結果是比條件的值小"); } } }
2.多個else if(){}語句
代碼如下:
public class mangElseDemo { public static void main(String[] arys) { int x = 10; if (x == 10) { System.out.print("value of x:" + x); } else if (x == 20) { System.out.print("value of x:" + x); } else if (x == 30) { System.out.print("value of x:" + x); } else { System.out.print("這是else語句!"); } } }
3.switch分支語句
代碼如下:
public class switchDemo { public static void main(String[] arys) { char grade = 'E'; switch (grade) { case 'A': System.out.print("優秀"); break; case 'B': case 'C': System.out.print("良好"); break; case 'D': System.out.print("及格"); break; case 'E': System.out.print("不及格"); break; } } }
多動手,豐衣足食,纔是硬道理!