✿✿323 2022-04-22 15:32 采纳率: 83.3%
浏览 71
已结题

这个十六进制该怎么弄才能让这个数字转换成字母呢

这是我的代码

public class zy14 {
    public static void main(String[] args) {
        Scanner r=new Scanner(System.in);
        int m=r.nextInt();
        int n=r.nextInt();
        System.out.println("shi jz"+"    "+"er jz"+"    "+"ba jz"+"    "+"sl jz");
        for(int i=m;i<=n;i++){
            System.out.print(i+"       ");
            ejz(i);
            System.out.print("       ");
            bjz(i);
            System.out.print("       ");
            sljz(i);
            System.out.println();
        }
        r.close();
    }
    public static void ejz(int i){
        Stack<Integer> s=new Stack<>();
        int e;
        while(i!=0){
            s.push(i%2);
            i=i/2;
        }
        while(!s.empty()){
            e=s.pop();
            System.out.print(e);
        }
        
    }
    public static void bjz(int i){
        Stack<Integer> s=new Stack<>();
        int e;
        while(i!=0){
            s.push(i%8);
            i=i/8;
        }
        while(!s.empty()){
            e=s.pop();
            System.out.print(e);
        }
    }
    public static void sljz(int i){
        Stack<Integer> s=new Stack<>();
        int m=0;int e;
        while(i!=0){
            if ((i%16) <= 9) {
                 m=i%16;
            }
            else{
               switch (i%16) {
                case 10:
                    System.out.print('A');
                    break;
                case 11:
                    System.out.print('B');
                    break;
                case 12:
                    System.out.print('C');
                    break;
                case 13:
                    System.out.print('D');
                    break;
                case 14:
                    System.out.print('E');
                    break;
                case 15:
                    System.out.print('F');
                    break;
                }
            }
            s.push(m);
            i=i/16;
        }
        while(!s.empty()){
            e=s.pop();
            System.out.print(e);
        }
    }
}

要达成的目标:

img

我的运行结果:

img

  • 写回答

4条回答 默认 最新

  • qq_25376897 2022-04-22 16:08
    关注

    你的问题在于,用了栈来存和取,但是中间10以上的,却直接输出了,顺序就乱了。我下面改成保存每一个字符,按栈的入和出

    
    public static void sljz(int i) {
            Stack<Character> s = new Stack<>();
            char c;
            int m;
            char e;
            while (i != 0) {
                m = i % 16;
                switch (m) {
                    case 10:
                        c = 'A';
                        break;
                    case 11:
                        c = 'B';
                        break;
                    case 12:
                        c = 'C';
                        break;
                    case 13:
                        c = 'D';
                        break;
                    case 14:
                        c = 'E';
                        break;
                    case 15:
                        c = 'F';
                        break;
                    default:
                        c = (char) (m+48);//加上48,因为ASCII编码 48 到 57 为字符 '0' ~ '9' 的编码
                }
                s.push(c);
                i = i / 16;
            }
            while (!s.empty()) {
                e = s.pop();
                System.out.print(e);
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月30日
  • 已采纳回答 4月22日
  • 创建了问题 4月22日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改