/*
* 需求:提示用户输入一个十进制数,然后将它转换为一个十六进制数
*/
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);
}
}
转载于:https://www.cnblogs.com/echoing/p/7878986.html