GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Others/TowerOfHanoiUsingRecursion.java at master · rpendrick/Java Expand file tree/ Copy pathTowerOfHanoiUsingRecursion.javaMore file actions 26 lines (23 loc) · 1.06 KB / Copy pathTowerOfHanoiUsingRecursion.javaFile metadata and controls26 lines (23 loc) · 1.06 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.util.Scanner; class TowerOfHanoi { public static void shift(int n, String startPole, String intermediatePole, String endPole) { if (n == 0) // if n becomes zero the program returns thus ending the loop. { return; } // Shift function is called in recursion for swapping the n-1 disc from the startPole to the intermediatePole shift(n - 1, startPole, endPole, intermediatePole); System.out.println("\nMove \"" + n + "\" from " + startPole + " --> " + endPole); // Result Printing // Shift function is called in recursion for swapping the n-1 disc from the intermediatePole to the endPole shift(n - 1, intermediatePole, startPole, endPole); } public static void main(String[] args) { System.out.print("Enter number of discs on Pole 1: "); Scanner scanner = new Scanner(System.in); int numberOfDiscs = scanner.nextInt(); //input of number of discs on pole 1 shift(numberOfDiscs, "Pole1", "Pole2", "Pole3"); //Shift function called } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |