package com21.pingkung.program; import javax.swing.JOptionPane; public class TowerOfHanoi { private static int count=0; public static void main(String[] args) { String input; int disk; input = JOptionPane.showInputDialog(null,"Enter the number of disk"); disk = Integer.parseInt(input); towerOfHanoi(disk,1,3,2); System.out.println("The step for move disk form 1 to 3 is "+count); } public static void towerOfHanoi(int nDisk, int from, int to, int temp){ if(nDisk==1) moveone(from,to); else{ towerOfHanoi(nDisk-1,from, temp, to); moveone(from,to); towerOfHanoi(nDisk-1,temp, to, from); } } public static void moveone(int from, int to) { // TODO Auto-generated method stub System.out.println(from+"------->"+to); count++; } }
Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.