参考了“https://blog.nowcoder.net/n/7460061f61cd434991d8add0e2dac172?f=comment”。 改动了一下,
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
scanner.close();
if (input == null || input.length() < 3) {
System.out.println("");
return;
}
input = input.substring(2).toLowerCase();
int len = input.length();
int res = 0;
for (int i = 0; i < len; i++) {
char curChar = input.charAt(i);
int curNum = 0;
if (curChar >= '0' && curChar <= '9') {
curNum = curChar - '0';
} else if (curChar >= 'a' && curChar <= 'f') {
curNum = curChar - 'a' + 10;
} else {
throw new RuntimeException("wrong input.");
}
res += curNum * Math.pow(16, len - i - 1);
}
System.out.println(res);
}
}