Java 打印十六进制数

Java 打印十六进制数,java,hex,Java,Hex,我想写一个程序,当输入一个0到100之间的整数时,它会打印一个十六进制数。我的代码有什么问题 import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n, p, q; n = input.nextInt(); p = n / 16;

我想写一个程序,当输入一个0到100之间的整数时,它会打印一个十六进制数。我的代码有什么问题

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n, p, q;
        n = input.nextInt();
        p = n / 16;
        q = n % 16;
        if (q == 10)
            System.out.println(p + "" + "a");
        else if (q == 11)
            System.out.println(p + "" + "b");
        else if (q == 12)
            System.out.println(p + "" + "c");
        else if (q == 13)
            System.out.println(p + "" + "d");
        else if (q == 14)
            System.out.println(p + "" + "e");
        else if (q == 15)
            System.out.println(p + "" + "f");
        else if (p == 0)
            System.out.println(q);
        else
            System.out.println(p + "" + q);
    }
}
我的代码有什么问题

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n, p, q;
        n = input.nextInt();
        p = n / 16;
        q = n % 16;
        if (q == 10)
            System.out.println(p + "" + "a");
        else if (q == 11)
            System.out.println(p + "" + "b");
        else if (q == 12)
            System.out.println(p + "" + "c");
        else if (q == 13)
            System.out.println(p + "" + "d");
        else if (q == 14)
            System.out.println(p + "" + "e");
        else if (q == 15)
            System.out.println(p + "" + "f");
        else if (p == 0)
            System.out.println(q);
        else
            System.out.println(p + "" + q);
    }
}

答案是:没有。您的代码为0到100之间的每个输入整数生成正确的输出。

当我运行您的代码并使用
23
78
作为输入进行测试时,我分别得到
17和
4e
作为结果,这是正确的,不是吗?你为什么认为有什么不对劲?您能提供一个示例并描述预期和实际的输出吗?实际上,这是我java测试的问题之一。我找不到一个错误作为答案。我猜是出了什么差错。