✿✿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日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么