题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
Scanner sc = new Scanner(System.in);
int res = 0; //定义最终值
while(sc.hasNextLine()){
String str = sc.nextLine();
String s1 =str.substring(2); //截取字符串,省的在下标上晕
System.out.println(s1);
for(int i=s1.length()-1;i>=0;--i){ //从右到左开始计算
char c = s1.charAt(i);
int num = 0; //记录每一位的十进制值
if(c>='0' && c<='9') //切忌一定要 >= <=
num = c-'0'; //char类型 转换成int技巧
else if( c>='A' && c<='F'){
num = c-'A'+10;
System.out.println(num);
}
else if( c>='a' && c<='f')
num = c-'a'+10;
res = (int) (res + num* Math.pow(16,s1.length()-i-1));
System.out.println(res);
}
System.out.println(res);
}
小天才公司福利 1199人发布