2 回答

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
("[g]et, [p]ut... 字符串被打印兩次,因?yàn)樵谀斎氩?Enter 后,for 循環(huán)會(huì)為您輸入的輸入運(yùn)行一次,再為“enter”運(yùn)行一次輸入后按下按鈕
根據(jù)你想要的,在 else if (choice.equals("p")) this elsed if 塊中減少 i
else if (choice.equals("p")){
//your code
i--;
}

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
我建議使用Recursion,“公式”要容易得多:這是代碼:
public class TowerOf_Hanoi {
public static void main(String [] args){
java.util.Scanner input=new java.util.Scanner(System.in);
System.out.print("Enter Number of Disks: ");
int numDisk=input.nextInt();
System.out.println("Moves are: ");
steps(numDisk,'A','B','C');
}
public static void steps(int n, char fromTower, char toTower, char auxTower){
//base case for Recursion
if(n==1) //if n=1 it will stop
System.out.println("Move disk "+n+" from "+fromTower+" to "+toTower);
else{
steps(n-1,fromTower,auxTower,toTower); //recursion
System.out.println("Move disk "+n+" from "+fromTower+" to "+toTower);
steps(n-1,auxTower,toTower,fromTower);
}
}
}
添加回答
舉報(bào)