Discuss / Java / 写的有点复杂,还有点小毛病,不知道哪错了

写的有点复杂,还有点小毛病,不知道哪错了

Topic source
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;
    }
}

T商人

#2 Created at ... [Delete] [Delete and Lock User]

更改前

更改后

while (stack != null) {    try {        str += stack.pop();    } catch (Exception e) {        break;    }}

T商人

#3 Created at ... [Delete] [Delete and Lock User]

更改前

while(stack != null){

str+=stack.pop();

}


  • 1

Reply