用java實現的十進制轉為2,8,16進制數,思路很簡單,就是除n取余
/**
* @(#)jinzhi.java
*
*
* @author CplusHua
* @version 1.00 2013/4/17
*/
import java.util.*;
public class jinzhi {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.println ("輸入一個十進制的數字,輸入0退出程序");
for(;;){
int m=s.nextInt();
if(0==m) return ;
eight(m);
sixteen(m);
binary(m);
}
}
public static void eight(int a){
int s[]=new int[100];
int i;
for(i=0;a!=0;i++){
s[i]=a%8;
a/=8;
}
i--;
System.out.print("八進制為");
for(;i>=0;i--){
System.out.print(s[i]);
}
System.out.println ();
}
public static void sixteen(int a){
char code[]={'A','B','C','D','E','F'};
int s[]=new int[100];
int i;
for(i=0;a!=0;i++){
s[i]=a%16;
a/=16;
}
i--;
System.out.print("十六進制為:");
for(;i>=0;i--){
if(s[i]<10)
System.out.print(s[i]);
else
System.out.print(code[s[i]-10]);
}
System.out.println ();
}
public static void binary(int a){
int s[]=new int[1000];
int i;
for(i=0;a!=0;i++){
s[i]=a%2;
a/=2;
}
i--;
System.out.print("二進制為:");
for(;i>=0;i--){
System.out.print(s[i]);
}
System.out.println ();
}
}