Need help getting things to loop correctly
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.
Code Java:
]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
Re: Need help getting things to loop correctly
First question: Why use a while() rather than a for() loop?
Quote:
having trouble getting this to work for every input except A>B
What does this mean?
Try debugging your code by adding print outs of ALL the variables values as the loop executes.
Re: Need help getting things to loop correctly
where is the body of mainLoop?
Re: Need help getting things to loop correctly
I'm confuzed because of this comment:
Quote:
}// end method mainLoop()
Re: Need help getting things to loop correctly
Seems the OP missed a couple of lines when he posted his code. Add }\n void mainLoop() {
after the call to mainLoop.
Re: Need help getting things to loop correctly
This solves something I think:
Code Java:
package GGG;
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();
}
void 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);
int tries=3;
// Now use "if" statements to change the values stored in variables
// stackA, stackB, and stackC
while (turnCounter < tries) {
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
}
turnCounter++;
System.out.println("Stacks A B C contain: ");
System.out.printf(" %2d %2d %2d \n", stackA, stackB, stackC);
if(turnCounter==tries){
continue;
}
System.out.print("Enter the stack from: ");
// 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
// Get destination stack letter
destinationStack = keyboard.next().toUpperCase().charAt(0);
}
}// end method mainLoop()
}// end
Re: Need help getting things to loop correctly
Are you trying to solve the OPs problem or are you trying to help the OP solve the problem?
If you're going to solve the problem at least explain what you did to find the problem and what you did to solve it.
Giving code doesn't really let the OP go thru the learning process. Will you be there the next time the OP doesn't know how to solve a problem?