十进制转十六进制

/*
 * 需求:提示用户输入一个十进制数,然后将它转换为一个十六进制数
 */
package com.test5;

import java.util.Scanner;

public class Dec2Hex {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入一个十进制数:");

		int decimal = input.nextInt();

		// 将十进制数转换为十六进制数

		String hex = "";
		while (decimal != 0) {
			int hexValue = decimal % 16;
			char hexDigit = (hexValue >= 0 && hexValue <= 9) ? (char) (hexValue + '0') : (char) (hexValue - 10 + 'A');
			hex = hexDigit + hex;
			decimal = decimal / 16;
		}
		System.out.println("十六进制数是:" + hex);

	}

}
posted @ 2017-11-22 14:21  微微_echo  阅读(494)  评论(0编辑  收藏  举报