如果你想通过 2 的幂来做到这一点,即基本的数学方式,在 php
<?php
$array = array(
0 => "Success",
1 => "File Not Found",
2 => "Could not create file",
4 => "Directory not found",
8 => "Directory could not be created",
16 => "Disk damaged"
);
$total_code = 7;
$error_code = 1;
while($total_code >= 1){
if($total_code % 2 == 1){
echo $array[$error_code]."<br>";
}
$total_code /= 2;
$error_code *= 2;
}
?>
在 Java 中
HashMap<Integer,String> errors = new HashMap<Integer, String>();
errors.put(0, "Success");
errors.put(1, "File Not Found");
errors.put(2, "Could not create file");
errors.put(4, "Directory not found");
errors.put(8, "Directory could not be created");
errors.put(16, "Disk damaged");
int total_code = 7;
int error_code = 1;
while(total_code >= 1) {
if(total_code % 2 == 1)
System.out.println(errors.get(error_code));
total_code /= 2;
error_code *= 2;
}