フラグの作成方法


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

ビット演算子を使う
public class BitSample {
 // フラグ
 public static final int A = 1;
 public static final int B = A << 1;
 public static final int C = B << 1;
 public static final int D = C << 1;
 public static final int E = D << 1;
 public static int FLG = 0;

 public static void flgUp(int flg) {
  FLG = FLG | flg;
 }

 public static void flgDown(int flg) {
  FLG = FLG ^ (FLG & flg);
 }

 public static boolean or(int flg) {
  return (FLG & flg) != 0;
 }

 public static boolean and(int flg) {
  return (FLG & flg) == flg;
 }

 public static boolean match(int flg) {
  return FLG == flg;
 }

 public static void main(String[] args) {
  flgUp(C | E);
  if (or(A | B) && or(C | D)) {
   System.out.println("(AまたはB) かつ (CまたはD) TRUE");
  } else {
   System.out.println("(AまたはB) かつ (CまたはD) FALSE");
  }
  if (and(A | B) || and(C | D)) {
   System.out.println("(AかつB) または (CかつD) TRUE");
  } else {
   System.out.println("(AかつB) または (CかつD) FALSE");
  }
 }
}
enumを使う
public class FlagSample {
 enum Color {
  black, aliceblue, darkcyan, lightyellow, coral, dimgray, lavender, teal, lightgoldenrodyellow, tomato, gray, lightsteelblue, darkslategray, lemonchiffon, orangered, darkgray, lightslategray, darkgreen, wheat, red, silver, slategray, green, burlywood, crimson, lightgrey, steelblue, forestgreen, tan, mediumvioletred, gainsboro, royalblue, seagreen, khaki, deeppink, whitesmoke, midnightblue, mediumseagreen, yellow, hotpink, white, navy, mediumaquamarine, gold, palevioletred, snow, darkblue, darkseagreen, orange, pink, ghostwhite, mediumblue, aquamarine, sandybrown, lightpink, floralwhite, blue, palegreen, darkorange, thistle, linen, dodgerblue, lightgreen, goldenrod, magenta, antiquewhite, cornflowerblue, springgreen, peru, fuchsia, papayawhip, deepskyblue, mediumspringgreen, darkgoldenrod, violet, blanchedalmond, lightskyblue, lawngreen, chocolate, plum, bisque, skyblue, chartreuse, sienna, orchid, moccasin, lightblue, greenyellow, saddlebrown, mediumorchid, navajowhite, powderblue, lime, maroon, darkorchid, peachpuff, paleturquoise, limegreen, darkred, darkviolet, mistyrose, lightcyan, yellowgreen, brown, darkmagenta, lavenderblush, cyan, darkolivegreen, firebrick, purple, seashell, aqua, olivedrab, indianred, indigo, oldlace, turquoise, olive, rosybrown, darkslateblue, ivory, mediumturquoise, darkkhaki, darksalmon, blueviolet, honeydew, darkturquoise, palegoldenrod, lightcoral, mediumpurple, mintcream, lightseagreen, cornsilk, salmon, slateblue, azure, cadetblue, beige, lightsalmon, mediumslateblue
 }

 private static EnumSet<Color> FLG = EnumSet.noneOf(Color.class);
 public static void main(String[] args) {
  System.out.println(Color.aqua.ordinal());
  
  FLG.add(Color.aqua);
  FLG.add(Color.black);
  
  EnumSet<Color> condition = EnumSet.of(Color.blue,Color.yellow);
  if(FLG.containsAll(condition)){
   System.out.println("blueかつyellow TRUE");
  }else{
   System.out.println("blueかつyellow FALSE");
  }
  
  if(FLG.contains(Color.aqua) || FLG.contains(Color.snow)){
   System.out.println("aquaまたはsnow TRUE");
  }else{
   System.out.println("aquaまたはsnow FALSE");
  }
  
 }
}
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。