您的位置:首页 > 编程语言 > Java开发

java处理汉诺塔问题

2008-05-09 15:26 225 查看
import java.util.Scanner;

public class HanoiTest {
public HanoiTest(){

}
public static void move(String from,String to){
System.out.println("move the top plate from "+from+" to "+to);
}
public static void execute(int n,String from,String temp,String to){
if(n==1){
move(from,to);
}else{
execute(n-1,from,to,temp);
move(from,to);
execute(n-1,temp,from,to);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=null;
try{
sc = new Scanner(System.in);
System.out.print("请输入汉诺塔上盘子的初始数目:");
while(sc.hasNext()){
String s=sc.nextLine();
if(!s.matches("//d+")){
System.out.println("输入错误,请输入数字!");
}
int n=Integer.parseInt(s);
execute(n,"A","B","C");
System.out.print("请输入汉诺塔上盘子的初始数目:");
}
}catch(Exception e){
e.printStackTrace();
}finally{
sc.close();
System.out.println("End!");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: