标签:math can 接受 art parse 十六进制 输出 next import
使用Java写出一个程序:接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入 )
import java.util.Scanner; public class number { private static final String A = "A"; private static final String B = "B"; private static final String C = "C"; private static final String D = "D"; private static final String E = "E"; private static final String F = "F"; public static int change(String string) { int[] data = new int[string.length() - 2]; int result = 0; for (int i = 0; i < string.length() - 2; i++) { String sub = string.substring(i + 2, i + 3); switch (sub) { case A: data[i] = 10; break; case B: data[i] = 11; break; case C: data[i] = 12; break; case D: data[i] = 13; break; case E: data[i] = 14; break; case F: data[i] = 15; break; default: data[i] = Integer.parseInt(sub); } result += data[i] * Math.pow(16, string.length() - 2 - i - 1); } return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String string = scanner.nextLine(); int result = change(string); System.out.println(result); } } }
标签:math can 接受 art parse 十六进制 输出 next import
原文地址:https://www.cnblogs.com/fcbyoung/p/12562703.html