类文件:Transition.java
Java代码实现:
import java.util.Scanner;
public class Transition {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int a=input.nextInt();
int i,m,b,k;
char[] sum=new char[10];
k=a;
char[] s={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
for(i=0;i<10;i++){
b = a % 16;
sum[i]=s[b];
a=a/16;
if(a==0)
break;
}
m = i;
System.out.print(k+"‘s hex representation is ");
for(;m>=0;m--)
System.out.printf("%c",sum[m]);
}
}
运行结果示例
eg1:输入15
eg2:输入300