Alright i'm having trouble getting my loop to continue after one try (i am using a loopcounter, it just might not be in the right spot.) You're supposed to get 3 turns. I'm also having trouble getting this to work for every input except A>B. Any suggestions? Instructions are in the program.
]import java.util.Scanner; // used for console input (from the keyboard) // Declare the class public class toothpickPuzzle { // Fields that can be accessed anywhere in the class go here Scanner keyboard = new Scanner( System.in); // used to read user input public static void main(String[] args) { // create an instance of this class toothpickPuzzle theSampleInstance = new toothpickPuzzle(); // call a non-static method to do everything else theSampleInstance.mainLoop(); // Display identifying information System.out.println( "Author: Eric Grunauer\n" + "Program: #2 Toothpick Puzzle\n" + "T.A.: Rigel Gjomemo\n" + "Date: September 10, 2010"); System.out.println(); System.out.println("Welcome to the Toothpick Puzzle. Think of having 3 piles of toothpicks\n" + "in front of you, where there are 24 toothpicks total:\n" + " \n" + " Stack: A B C\n" + " Number of Toothpicks: 11 7 6\n" + " \n" + "The goal is to create 3 piles of 8 toothpicks in exactly 3 moves. A move\n" + "consists of moving toothpicks from one stack to a second stack, where the\n" + "number of toothpicks moved is exactly the number that is in the destination\n" + "stack. In other words, to move from stack B (7 toothpicks) to stack C (6)\n" + "as shown above, we would move 6 from B to C, leaving us with 1 in B and 12\n" + "in stack C."); System.out.println(); System.out.println(); System.out.println("Here we go..."); System.out.println(); System.out.println(); // declare variables used in the program Scanner keyboard = new Scanner( System.in); // declare the variables to store the number of toothpicks in each stack int stackA = 11; int stackB = 7; int stackC = 6; int turnCounter = 0; // display original stack values System.out.println(" Stack: A B C"); System.out.printf("Number of Toothpicks: %2d %2d %2d \n", stackA, stackB, stackC); System.out.println(); System.out.print("Enter the stack from: "); char sourceStack = ' '; // // Get the user input, convert it all to upper case, and retrieve the first character. // This first character should be 'A', 'B', or 'C', representing one of the stacks. sourceStack = keyboard.next().toUpperCase().charAt(0); System.out.print("Enter the stack to: "); // repeat for the second stack char destinationStack = ' '; // Get destination stack letter destinationStack = keyboard.next().toUpperCase().charAt(0); // Now use "if" statements to change the values stored in variables stackA, stackB, and stackC while (turnCounter < 3) { if( sourceStack == 'A' && destinationStack == 'C') { int numberToMove = stackC; // store how many should be moved stackA = stackA - numberToMove; // deduct from the source stack stackC = stackC + numberToMove; // move into the destination stack } if( sourceStack == 'A' && destinationStack == 'B') { int numberToMove = stackB; // store how many should be moved stackA = stackA - numberToMove; // deduct from the source stack stackB = stackB + numberToMove; // move into the destination stack } if( sourceStack == 'B' && destinationStack == 'A') { int numberToMove = stackA; // store how many should be moved stackB = stackB - numberToMove; // deduct from the source stack stackA = stackA + numberToMove; // move into the destination stack } if( sourceStack == 'B' && destinationStack == 'C') { int numberToMove = stackC; // store how many should be moved stackB = stackB - numberToMove; // deduct from the source stack stackC = stackC + numberToMove; // move into the destination stack } if( sourceStack == 'C' && destinationStack == 'B') { int numberToMove = stackB; // store how many should be moved stackC = stackC - numberToMove; // deduct from the source stack stackB = stackB + numberToMove; // move into the destination stack } if( sourceStack == 'C' && destinationStack == 'A') { int numberToMove = stackA; // store how many should be moved stackC = stackC - numberToMove; // deduct from the source stack stackA = stackA + numberToMove; // move into the destination stack } System.out.println("Stacks A B C contain: "); System.out.printf(" %2d %2d %2d \n", stackA, stackB, stackC); turnCounter++; } }//end method mainLoop() }//end