我的代码中的错误是,即使数组中存在“A”、“B”、“C”,内部循环内的 if 条件也无法识别它们。请注意,ABC 来自已使用 String.split(""); 拆分的字符串;java的方法
public class Hexadecimal
{
public static void main(String[] args)
{
String hex = "ABC";
hToD(hex);
}
public static void hToD(String hexa)
{
String[] hexadecimal = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String[] value = hexa.split("");
int deci = 0;
int bit =0;
for(int i = 0; i<value.length;i++)
{
for(int j = 0; j<hexadecimal.length;j++)
{
if(value[i] == hexadecimal[j])
{
deci = deci + compute(j,bit++);
System.out.println(deci);
}
}
}
}
public static int compute(int digit,int bit)
{
int ans = 0;
if(bit == 0)
{
ans = digit * (1);
}else
{
ans = digit * (16 * bit);
}
return ans;
}
}