经典算法题-十六进制转化十进制
Posted fcbyoung
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了经典算法题-十六进制转化十进制相关的知识,希望对你有一定的参考价值。
使用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); } } }
以上是关于经典算法题-十六进制转化十进制的主要内容,如果未能解决你的问题,请参考以下文章