每当我输入 2-36 的基数时,我都想要我的代码,它可以输出一个十进制数字。这是我的代码
package code;
public class solution{
public static int x2ten(String s, int base){
String[] bits = s.split("");
int result = 0;
for(int i=0; i < bits.length; i++ ){
int val = (Integer.parseInt(bits[bits.length-1-i])) * ((int)Math.pow(base,i));
val = charToInt(Integer.parseInt(bits[bits.length-1-i]))* ((int)Math.pow(base,i));
result += val;
}
return result;
}
public static int charToInt(char c){
char[] characters = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G',
'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for(int j = 0; j<characters.length; j++){
if(characters[j] == c){
return j;
}
}
return -1;
}
public static void main(String[] args)
{
System.out.println(x2ten("1101",2));
}
}
一开始出现charToInt有个问题,说我应该把charToInt(char c)改成charToInt(int c)。但是 charToInt 的目的是转换我输入的任何字符,如 1A,然后它可以输出十进制的 26。