题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
思路: 1.从键盘输入流获取字符串,用substring(2,substring.length-1)方法截取子字符串;
2.用for循环逐个获取每个位上的字符;
3.if判断每个字符是数字0-9 or 大写字母A-Z or 小写字母a-z;
4.对应进行求和,最后输出结果即可;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
String target = str.substring(2,str.length());
int result = 0;
int power = 1;
for(int i = target.length()-1;i >= 0;i--){
char c = target.charAt(i);
if(c >= '0' && c <= '9'){
result += (c - '0')*power;
}
if(c >= 'A' && c <= 'F'){
result += (c - 'A' +10)*power;
}
if(c >= 'a' && c <= 'f'){
result += (c - 'a' +10)*power;
}
power *= 16;
}
System.out.println(result);
}
sc.close();
}
} 