漢諾塔算法

package test;java

import javax.swing.JOptionPane;input

 

public class HanNuoTa {      io

  private static final String DISK_B = "diskB";    class

  private static final String DISK_A = "diskA";   test

  private static final String DISK_C = "diskC";      import

  static String from = DISK_A;   im

  static String to = DISK_C;   static

  static String mid = DISK_B;        di

 

public static void main(String args[]){    swing

  String input = JOptionPane.showInputDialog("請輸入你的輪盤數量");    

  int num = Integer.parseInt(input);    

  move(num, from, mid, to);      

 }         

private static void move (int num,String from2,String mid2,String to2){    

  if(num ==  1){     

    System.out.println("move disk 1 from "+from2+" to "+to2);    

  }else{     

    move(num -1,from2,to2,mid2);     

    System.out.println("move disk "+num+" from "+from2+" to "+to2);       

    move(num-1,mid2,from2,to2);    

    }  

   }

 }

相關文章
相關標籤/搜索