题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = ""; while (s.length() == 0) { s = in.nextLine().toUpperCase(); } String substring = s.substring(2); char[] chars = substring.toCharArray(); double result = 0; int index = 1; for (char aChar : chars) { result = result + convertNum(aChar) * Math.pow(16d, chars.length - index++); } System.out.println((int)result); } public static int convertNum(char s) { if (s == 'A') { return 10; } else if (s == 'B') { return 11; } else if (s == 'C') { return 12; } else if (s == 'D') { return 13; } else if (s == 'E') { return 14; } else if (s == 'F') { return 15; } return Integer.parseInt(String.valueOf(s)); } }