面试题:输入一个十进制整数,将这个数字转化成对应的十五进制数(在十五进制中,A表示10,B表示11,C表示12,D表示13, E表示14),请写入转换程序。例如:235表示为10A;

如上是本人一位朋友的公司出的研发小测试,小朋友是一脸懵逼啊!

 

分析:进制转换思路:10进制除以15商和余数,反复拿商除以15获得商和余数,类推,知道商为0,停止;

以235表示为10A为例分析:

235/15 = 15 余数10

15/15 = 1 余数 0

1/15 = 0 余数 1

得到数据10, 0, 1, 反序结果为1, 0, 10, 对比结果: 1-》1, 0-》0, 10-》A, 所以235表示的十五进制数为:10A;

 

具体实现如下:

@Test
	public void test() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入十进制数字:");
		/**
		 * Integer.parseInt(scanner.next(), 10); 10 代表的默认是10进制的
		 * Character.MIN_RADIX=2和Character.MAX_RADIX=36
		 * parseInt(String s, int radix)参数中radix的范围是在2~36之间,超出范围会抛异常。其中s的长度也不能超出7,否则也会抛异常。
		 */
		Integer in = Integer.parseInt(scanner.next(), 10);
		List<Object> list = new ArrayList<Object>();
		while (true) {
			list.add(in % 15); // 取余数
			in = in / 15; // 取余数
			if (in == 0) { // 停止循环
				break;
			}
		}
		StringBuffer str = new StringBuffer(); // 拼接字符串
		// 倒排
		for (int i = list.size() - 1; i >= 0; i--) {
			int number = (int) list.get(i);
			if (number == 10) {
				str.append("A");
			} else if (number == 11) {
				str.append("B");
			} else if (number == 12) {
				str.append("C");
			} else if (number == 13) {
				str.append("D");
			} else if (i == 14) {
				str.append("E");
			} else {
				str.append(number);
			}
		}
		System.out.println(str);
		//关流
		scanner.close(); 
	}

parseInt(String s, int radix)源码:

    if (s == null) {
        throw new NumberFormatException("null");
    }

    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
    }

    if (radix > Character.MAX_RADIX) {
         throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
    }


    /**
     * The minimum radix available for conversion to and from strings.
     * The constant value of this field is the smallest value permitted
     * for the radix argument in radix-conversion methods such as the
     * {@code digit} method, the {@code forDigit} method, and the
     * {@code toString} method of class {@code Integer}.
     *
     * @see     Character#digit(char, int)
     * @see     Character#forDigit(int, int)
     * @see     Integer#toString(int, int)
     * @see     Integer#valueOf(String)
     */
    public static final int MIN_RADIX = 2;

    /**
     * The maximum radix available for conversion to and from strings.
     * The constant value of this field is the largest value permitted
     * for the radix argument in radix-conversion methods such as the
     * {@code digit} method, the {@code forDigit} method, and the
     * {@code toString} method of class {@code Integer}.
     *
     * @see     Character#digit(char, int)
     * @see     Character#forDigit(int, int)
     * @see     Integer#toString(int, int)
     * @see     Integer#valueOf(String)
     */
    public static final int MAX_RADIX = 36;

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值