我想通過使用轉換將基數10數字轉換爲任何基數。現在這是我提出的代碼。我有一種悲傷的感覺,這可能是完全錯誤的。下面的圖片是這個過程應該如何發生的例子。Java - 遞歸程序 - 將基數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;
}
}
這功課呢?如果是這樣,它應該標記[家庭作業]標籤。 – 2012-04-17 03:20:32
你應該定義一個基本情況,比如說,當你的數字小於你的基數,並且遞歸部分,比方說,獲得轉換後數字 – 2012-04-17 03:21:04
的下一個數字的操作,感謝luiggi,Idk爲什麼我認爲== 0是一個好主意。我不認爲現在甚至需要我看它 – user878034 2012-04-17 03:35:53