「Java」switch文を使うサンプル

構文
switch(条件式}{
case 定数1:処理1;
case 定数2:処理2 ;
default:処理
}
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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));
}
}
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)); } }
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

Java

Posted by arkgame