我正在尝试使用转换将基数为 10 的数字转换为任何基数。现在这是我想出的代码。我有一种悲伤的感觉,这可能是完全错误的。下图是如何进行此过程的示例。
http://i854.photobucket.com/albums/ab107/tonytauart/rrrr.png
public static void main(String[] args) {
int base;
int number;
Scanner console = new Scanner(System.in);
System.out.println("Please enter the base");
base = console.nextInt();
System.out.println("Please enter the Number you would like to convert");
number = console.nextInt();
System.out.println(Converter(base, number));
}
public static int Converter(int Nbase, int Nnumber){
int answer;
int Rcontainer =0;
int cnt = 0;
int multiplier;
int temp;
double exp;
if(Nnumber/Nbase == 0){
cnt++;
exp = Math.pow(10,cnt);
multiplier = (int)exp;
answer = (Nnumber%Nbase)* multiplier + Rcontainer;
}
else
{
exp = Math.pow(10,cnt);
multiplier = (int)exp;
cnt++;
temp = Rcontainer;
Rcontainer = (Nnumber%Nbase)* multiplier + temp;
Nnumber = Nnumber/Nbase;
answer = Converter(Nbase,Nnumber);
}
return answer;
}
}