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

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

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

 

分析:进制转换思路: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 list = new ArrayList();
		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;

 

你可能感兴趣的:(Java)