Hi this is a towers of hanoi puzzle program. Right now I am having trouble with getting the program to solve for 3 discs if when prompted 'Enter the number of discs' is blank (i.e. enter key is hit without inputting any integer value).
My if/else statement inside my hanoi method is where I think the problem is. I commented out where I think the problem is. How do I get the program to just solve for just 3 discs if nothing is input when prompted for 'Enter the number of discs'?
Code:
import java.util.Scanner; public class TowerOfHanoi4 { static int moves = 0; static boolean displayMoves = false; public static void main(String[] args) { System.out.println("Enter the Number of Discs : "); Scanner scanner = new Scanner(System.in); int iHeight = scanner.nextInt(); char source = 'S', auxiliary = 'D', destination = 'A'; // name poles or // 'Needles' // int blank = null; System.out.println("Press 'v' or 'V' for a list of moves"); Scanner show = new Scanner(System.in); String c = show.next(); displayMoves = c.equalsIgnoreCase("v"); hanoi(iHeight, source, destination, auxiliary); System.out.println(" Total Moves : " + moves); } static void hanoi(int height, char source, char destination, char auxiliary) { if (height >= 1) { hanoi(height - 1, source, auxiliary, destination); if (displayMoves) { System.out.println(" Move disc from needle " + source + " to " + destination); } moves++; hanoi(height - 1, auxiliary, destination, source); } // else (height = 3) { //I think the problem // hanoi(height - 1, source, auxiliary, destination);//Lies with this // moves++; //else // hanoi(height - 1, auxiliary, destination, source);//statement // } } }