0
因此,我正在爲我的算法課程編寫一個基本的河內塔計劃,我只是重新閱讀說明書,希望在每步後顯示每個塔樓的內容(例如A = 4,3,2,1),其中4個是A座的底盤,1座是最高的。我已經有計劃寫下面的代碼:將整數值轉換爲多個字符串?
import java.util.Scanner;
public class Driver {
public static void main(String[]args) {
Scanner keyboard = new Scanner (System.in);
int discs;
System.out.println("How many discs are in the starting tower? ");
discs = keyboard.nextInt();
keyboard.close();
Hanoi(discs, 'A', 'B', 'C');
}
public static void Hanoi(int n, char Tower1, char Tower2, char Tower3){
if (n>0) {
Hanoi(n-1, Tower1, Tower2, Tower3);
if(n%2 == 0){
System.out.println("Move disc from " + Tower1 + " to " + Tower2);
System.out.println("Move disc from " + Tower1 + " to " + Tower3);
System.out.println("Move disc from " + Tower2 + " to " + Tower3);
}
else{
System.out.println("Move disc from " + Tower1 + " to " + Tower3);
System.out.println("Move disc from " + Tower1 + " to " + Tower2);
System.out.println("Move disc from " + Tower3 + " to " + Tower2);
}
}
}
可能幾架失蹤由於有其他的代碼不屬於此。
我的問題是是否可以取n /盤的值(從代碼)並將其轉換爲單個數字來填充多個陣列的輸出如下所示:
實施例:n = 4的 數組[4,3,2,1](Tower1)
,並能夠移動這些值作爲塔遊戲會對你做
我喜歡的代碼,我和一個較低的水平程序員會喜歡不必刪除它,但我願意傾聽誰能幫助我。
你想要做的只是將字符轉換爲數字嗎? – Ari 2014-09-30 16:34:07