Java练习题。
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);//使用Scanner类创建它的对象,以读取来自System.in的输入
System.out.print("Enter a decimal number:");//输入提示词
int decimal = input.nextInt();//用户输入一个十进制数
//将十进制数转换成十六进制形式的字符串
String hex = "";
while(decimal != 0) {
int hexValue = decimal % 16;
char hexDidit = (hexValue <= 9 && hexValue >= 0)?
(char)(hexValue+'0'):(char)(hexValue - 10 + 'A');
hex = hexDidit + hex;
decimal = decimal/16;
}
input.close();
//输出结果。
System.out.println("The hex number is " + hex);
}
}