import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
String s = in.next();
long temp = 0;
for(int i = 0;i<s.length();i++) {
char c = s.charAt(i);
switch (c) {
case '0':
temp = (long) (temp+((long)0*Math.pow(16, s.length()-1-i)));
break;
case '1':
temp = (long) (temp+((long)1*Math.pow(16, s.length()-1-i)));
break;
case '2':
temp = (long) (temp+((long)2*Math.pow(16, s.length()-1-i)));
break;
case '3':
temp = (long) (temp+((long)3*Math.pow(16, s.length()-1-i)));
break;
case '4':
temp = (long)(temp+((long)4*Math.pow(16, s.length()-1-i)));
break;
case '5':
temp = (long) (temp+((long)5*Math.pow(16, s.length()-1-i)));
break;
case '6':
temp = (long)(temp+((long)6*Math.pow(16, s.length()-1-i)));
break;
case '7':
temp = (long) (temp+((long)7*Math.pow(16, s.length()-1-i)));
break;
case '8':
temp = (long) (temp+((long)8*Math.pow(16, s.length()-1-i)));
break;
case '9':
temp = (long) (temp+((long)9*Math.pow(16, s.length()-1-i)));
break;
case 'A':
temp = (long) (temp+((long)10*Math.pow(16, s.length()-1-i)));
break;
case 'B':
temp = (long) (temp+((long)11*Math.pow(16, s.length()-1-i)));
break;
case 'C':
temp = (long) (temp+((long)12*Math.pow(16, s.length()-1-i)));
break;
case 'D':
temp = (long) (temp+((long)13*Math.pow(16, s.length()-1-i)));
break;
case 'E':
temp = (long) (temp+((long)14*Math.pow(16, s.length()-1-i)));
break;
case 'F':
temp = (long) (temp+((long)15*Math.pow(16, s.length()-1-i)));
break;
}
}
System.out.println(temp);
}}}
调试了很多次,都是因为自己思考不全面加上粗心造成,为了养成好的思考习惯,总结如下
1.首先因为搞错十六进制转换成为八进制的概念不熟悉,让每一个十六进制的数字变为十进制后直接变为字符串相加(粗心)
2.其次是没有加上十六进制中还包括0的情况(思考不全面)
3.因为charAt是从字符串左边的0位开始,所以对于每一位十六进制的数字,都应该乘以十六进制数长度-1-i的次方,而不是i次方
拿到一个题目之后切记不能直接动手去做,而是应该想好大概框架,列举出实现方法的不同情况之后再下手,以前刚开始的习惯还好些,因为做的多了反而有时候不喜欢想完全再动手,一定要脚踏实地一个一个脚印走,切勿急躁。