代码如下:
import java.util.Scanner;public class Dex2Hex {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("请输入一个十进制数:");int decimal/*十进制*/ = input.nextInt();String hex = "";while(decimal != 0) {int hexValue = decimal % 16;char hexDigit = (0 <= hexValue && hexValue <= 9)?(char)(hexValue + '0') : (char)(hexValue - 10 + 'A');hex/*十六进制*/ = hexDigit + hex;decimal = decimal / 16;}System.out.println("该十六进制数为:" + hex);}}
程序运行结果:
请输入一个十进制数:15
该十六进制数为:F