Trying to get user input to work correctly
I have a towers of hanoi puzzle program that is getting closer to being done. My problem right now is trying to get my input from the user to work properly.
If they type 'v' or 'V', then the steps to solve the puzzle will be displayed (Hence the output would be 'Move Disc from S to D' and so forth) . Else, if the user does not type 'v' or 'V', then the program proceeds with in solving the puzzle, displaying the Total moves but not displaying the steps.
The problem I am having is the options aren't working like they are supposed to.
Now the only thing wrong is when the user input's 'v' or 'V', the moves are not displayed correctly. Output:
Code :
Enter the min number of discs :
2
Press 'v' or 'V' for a list of moves
v
Move disc from needle S to A
Total Moves : 3
How can I accomplish having the moves displayed if the user types 'v' or 'V', and if the user types some other than this the output just displays the 'Total Moves'?
Here is my code:
Code :
import java.util.*;
import java.util.Scanner;
public class TowerOfHanoi4 {
static int moves=0;
public static void main(String[] args) {
System.out.println("Enter the min number of discs : ");
Scanner scanner = new Scanner(System.in);
int iHtMn = scanner.nextInt(); //iHeightMin
char source='S', auxiliary='D', destination='A'; //name poles or 'Needles'
System.out.println("Press 'v' or 'V' for a list of moves");
Scanner show = new Scanner(System.in);
String c = show.next();
// char lstep='v', lsteps='V'; // grab option v or V
if (c.equalsIgnoreCase("v")){ //if option is not v or V, execute code and only display total moves
hanoi(iHtMn, source, destination, auxiliary); //else, user typed v or V and moves are displayed
System.out.println(" Move disc from needle "+source+" to "+destination);
System.out.println(" Total Moves : "+moves);
} else {
hanoi(iHtMn, source, destination, auxiliary);
System.out.println(" Total Moves : "+moves);
}
}
public static void hanoi(int htmn,char source,char destination,char auxiliary)
{
if (htmn >=1)
{
hanoi(htmn-1, source, auxiliary, destination); // move n-1 disks from source to auxilary
// System.out.println(" Move disc from needle "+source+" to "+destination); // move nth disk to destination
moves++;
hanoi(htmn-1, auxiliary, destination, source);//move n-1 disks from auxiliary to Destination
}
}
}
Re: Trying to get user input to work correctly
What problems are you having? Are you working on the design or on coding from the design?
Do you have any specific questions?
Re: Trying to get user input to work correctly
How do I get the user option 'v' or 'V' to display all of the moves? And in the same question, if they do not type 'v' or 'V', how do I just execute the program without displaying the moves??
Re: Trying to get user input to work correctly
If you read user input as a String you can use the equalsIgnoreCase method.
Re: Trying to get user input to work correctly
Updated code above!!! Only problem I am having now is displaying the moves when the option v or V is typed.
Re: Trying to get user input to work correctly
Use a boolean variable to remember that the user has entered the v/V and test it to know if a move is to be displayed.
Re: Trying to get user input to work correctly
just use another variable when you call hanoi function...
Code java:
import java.util.Scanner;
public class TowerOfHanoi4 {
static int moves=0;
public static void main(String[] args) {
System.out.println("Enter the min number of discs : ");
Scanner scanner = new Scanner(System.in);
int iHtMn = scanner.nextInt(); //iHeightMin
char source='S', auxiliary='A', destination='D'; //name poles or 'Needles'
System.out.println("Press 'v' or 'V' for a list of moves");
Scanner show = new Scanner(System.in);
String c = show.next();
hanoi(iHtMn, source, destination, auxiliary,c);
System.out.println(" Total Moves : "+moves);
show.close();
scanner.close();
}
public static void hanoi(int htmn,char source,char destination,char auxiliary, String c){
if (htmn >=1){
hanoi(htmn-1, source, auxiliary, destination,c); // move n-1 disks from source to auxiliary
if(c.equalsIgnoreCase("v"))
System.out.println(" Move disc from needle "+source+" to "+destination); // move nth disk to destination
moves++;
hanoi(htmn-1, auxiliary, destination, source,c); //move n-1 disks from auxiliary to Destination
}
}
}
Re: Trying to get user input to work correctly
@aprabhat
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.