import java.util.*; public class Test { public static void main(String[] args) { String hex = toHex(12500); if (hex.equalsIgnoreCase("30D4")) { System.out.println("测试通过"); } else { System.out.println("测试失败"); } } static String toHex(int n) { Map<Integer, String> map = new HashMap<>(); map.put(0, "0"); map.put(1, "1"); map.put(2, "2"); map.put(3, "3"); map.put(4, "4"); map.put(5, "5"); map.put(6, "6"); map.put(7, "7"); map.put(8, "8"); map.put(9, "9"); map.put(10, "A"); map.put(11, "B"); map.put(12, "C"); map.put(13, "D"); map.put(14, "E"); map.put(15, "F"); Deque<String> stack = new LinkedList<>(); while (n != 0) { int remainder = n % 16; stack.push(map.get(remainder)); n = n / 16; } String str = ""; while (stack != null) { str += stack.pop(); } return str; } }
更改前
更改后
while (stack != null) { try { str += stack.pop(); } catch (Exception e) { break; }}
while(stack != null){
str+=stack.pop();
}
Sign in to make a reply
征服天堂jj