Discuss / Java / 练习1 有点蠢

练习1 有点蠢

Topic source

static String toHex(int n) {

Stack<String> stack = new Stack<>();

while (n != 0) {
int k = n % 16;
n = n / 16;

if (k < 10) {
stack.push(Integer.toString(k));
}
if (k == 10) {
stack.push("A");
}
if (k == 11) {
stack.push("B");
}
if (k == 12) {
stack.push("C");
}
if (k == 13) {
stack.push("D");
}
if (k == 14) {
stack.push("E");
}
if (k == 15) {
stack.push("F");
}
}

String result = "";
int size = stack.size();
for (int i = 0; i < size; i++) {
String pop = stack.pop();
result += pop;
}

return result;
}


  • 1

Reply