「Java」switch文を使うサンプル
構文
switch(条件式}{
case 定数1:処理1;
case 定数2:処理2 ;
default:処理
}
Javaコード
package com.arkgame.study;
public class SwitchDemo {
public static final boolean flgA = true;
public static final boolean flgB = false;
public static boolean showRes(String target) {
switch (target) {
case "AA10":
return flgA;
case "BB20":
return flgA;
default:
return flgB;
}
}
public static void main(String[] args) {
String strA = "AA10";
String strB = "B01";
String strC = "BB20";
System.out.println("変数Aの関数処理後結果:" + showRes(strA));
System.out.println("変数Bの関数処理後結果:" + showRes(strB));
System.out.println("変数Cの関数処理後結果:" + showRes(strC));
}
}
実行結果
変数Aの関数処理後結果:true
変数Bの関数処理後結果:false
変数Cの関数処理後結果:true