From 65774b4aa8b56f999f78057342ba787cbfc65fe1 Mon Sep 17 00:00:00 2001 From: shellhub Date: Sat, 11 Jul 2020 10:37:01 +0800 Subject: [PATCH] format code --- Others/TowerOfHanoi.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Others/TowerOfHanoi.java b/Others/TowerOfHanoi.java index db319595..5648b3d2 100644 --- a/Others/TowerOfHanoi.java +++ b/Others/TowerOfHanoi.java @@ -5,16 +5,13 @@ import java.util.Scanner; class TowerOfHanoi { public static void shift(int n, String startPole, String intermediatePole, String endPole) { // if n becomes zero the program returns thus ending the loop. - if (n == 0) { - return; + if (n != 0) { + // 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.format("Move %d from %s to %s\n", n, 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); } - - - // 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) {