java 解决汉诺塔问题

//汉诺塔问题
//HanYang 2016/10/15

import java.util.Scanner;

//输出
public class Hanuota {
 public static void Show(String a,String b){
  System.out.print(" " + a + "->" + b + " " );
 }

//从a移到c
    public static void Fun(int n, String a,String b,String c){
     if(n<1)
      System.out.println("input error,please input integer(bigger than 1):");
     else if(n == 1)
      System.out.print( " " + a + "->" + c );
     else{
      Fun(n-1,a,c,b);
      Show(a,c);
      Fun(n-1,b,a,c);
     } 
    }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
      Scanner in = new Scanner(System.in);
      int m;
      System.out.println( "please input the number of Hanoi:");
      m = in.nextInt();
      String a,b,c;
      System.out.println( "please input the name of three pillars:");
      a = in.next();
      b = in.next();
      c = in.next();
      Fun(m,a,b,c);
 }

}

上一篇:java:类集框架


下一篇:java类集框架图(google找的,备个份)