笔试编程题-2017电信

1 进制转换 10进制转换为16进制

package dianxin;

import java.util.Scanner;

public class 进制 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            int num = Integer.parseInt(str);
            String result = Integer.toHexString(num);
            System.out.println(result);

        }
    }

}

//
 十进制转化为十六进制,结果为C8。

Integer.toHexString(200);



//
 十六进制转化为十进制,结果140Integer.parseInt("8C",16);

不知道为啥,没有AC,然后自己写了方法

package dianxin;

import java.util.Scanner;

public class jinzhi{

public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()){
        String str = sc.nextLine();
        int num = Integer.parseInt(str);
        //String result = Integer.toHexString(num);
        t1(num);
        //System.out.println(result);

    }
}
private static int t1(int a){
    String str = "";
    //1:用a去除以16,得到商和余数
    int s = a / 16;
    int yushu = a % 16;
    str = "" + shuZiToZiMu(yushu);
    while(s>0){
        //2:继续用商除以16 得到商和余数
        yushu = s % 16;
        s = s / 16;
        //3:如果商为0,那么就终止
      //4:把所有的余数倒数排列
      str = shuZiToZiMu(yushu) +str;
    }
    System.out.println(str);

    //将16进制转化为10进制
    return Integer.parseInt(str,16);
}
private static String shuZiToZiMu(int a){
    switch(a){
        case 10 : return("A");
        case 11 : return("B");
        case 12 : return("C");
        case 13 : return("D");
        case 14 : return("E");
        case 15 : return("F");
    }
    return ""+a;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值