-3
import java.io.*;
import java.util.Scanner;


public class Helloworld{
public static BufferedReader input=new BufferedReader(new InputStreamReader(System.in));

public static void main(String[]args) throws IOException{

    System.out.print('\u000C');
    //variables
     String lev,tower,disk;
     int [] slot;
     slot= new int [9];

     slot[0]=1;
     slot[3]=2;
     slot[6]=3;



  System.out.println("Press enter to continue...");
  Scanner keyboard = new Scanner(System.in);
  keyboard.nextLine();
  System.out.print('\u000C');        
    System.out.println("Select Levels:");
    System.out.println("[1]Level 1 <3 numbers>");
    System.out.println("[2]Level 2 <4 numbers>");
    System.out.println("[3]Level 3 <5 numbers>");
    System.out.println("[4]Instructions");
    System.out.print("Choose one: ");
    lev =(input.readLine());

 if (lev.equals("4")){

        System.out.println("* You must transfer all numbers from tower 1 - tower 3 in ascending order.");
        System.out.println("* For every move you can only move one number to    a certain.");
        System.out.println("* The number on the top of the tower are the one       that you can move only.");

 }


else if (lev.equals("1")){
  //loop
while (true) {
System.out.print('\u000C');
 //display
System.out.println("  A  ,  B  ,  C  ");
System.out.println("| " +slot[0]+ "  |  " +slot [1]+  "  |  " +slot[2]+ "  |");
System.out.println("| " +slot[3]+ "  |  " +slot [4]+  "  |  " +slot[5]+ "  |");
System.out.println("| " +slot[6]+ "  |  " +slot [7]+  "  |  " +slot[8]+ "  |");
// input a disk for user
System.out.println("What number do you want to move <1, 2, 3, e to exit>:      ");
disk = (input.readLine());
if (disk.equals("e")) {
    break;
}
// input tower where the disk will contain.
System.out.println("What tower do you want to put <A, B, C>: ");
tower = (input.readLine());
   // Possible moves for user
if (disk.equals("1") && tower.equals("b")){
    slot[7]=slot[0]; 
    slot[0]=0;
    System.out.println("Hello");
}
else if (disk.equals("2") && tower.equals("c")){
    slot[8]=slot[3]; slot[3]=0;
}
if (disk.equals("3") && tower.equals("b")){
    slot[5]=slot[7]; slot[7]=0;
}
else if (disk.equals("1") && tower.equals("b")){
    slot[5]=slot[4]; slot[4]=0;
}
 else if (disk.equals("1") && tower.equals("a")){
    slot[6]=slot[5]; slot[5]=0;
}
else if (disk.equals("2") && tower.equals("b")){
    slot[4]=slot[8]; slot[8]=0;
}
else if (disk.equals("2") && tower.equals("a")){
    slot[2]=slot[6]; slot[6]=0;
}
else if (disk.equals("1") && tower.equals("a")){
    slot[3]=slot[4]; slot[4]=0;
}//
else if (disk.equals("1") && tower.equals("b")){
    slot[2]=slot[6]; slot[6]=0;
}
else if (disk.equals("3") && tower.equals("c")){
    slot[8]=slot[7]; slot[7]=0;
}
else if (disk.equals("1") && tower.equals("b")){
    slot[7]=slot[3]; slot[3]=0;
}
else if (disk.equals("2") && tower.equals("c")){
    slot[5]=slot[6]; slot[6]=0;
}
else if (disk.equals("1") && tower.equals("c")){
    slot[2]=slot[7]; slot[7]=0;
}
}
}
}
}

I dont know whats the error.I cant put the 1 in the top of 2. When i put the 1 on the top of the 2 the 2 becomes 0. That is a tower of hanoi. I want to create a game named tower of hanoi. I want to allow the user to choose what he/she want to move. Thats the first input. The second input is i want to user to what tower he/she wants to put.

4 Answers4

1

NEVER use GOTO!

If i understand you correctly, you want to prompt the user for new input after each step. so you would want to put this in a loop

something like

while(continueProgramm){
    System.out.println("What number do you want to move <1, 2, 3>: ");
    disk =(input.readLine());
    System.out.println("What tower do you want to put <A, B, C>: ");
    tower =(input.readLine());

   //handle your input here ...
}
bwright
  • 896
  • 11
  • 29
  • "NEVER use GOTO!" <-- this is an overly gross generalization. Have you ever read the Linux kernel source code? If not, have a look at it. And try and imagine what error recovery would look like _without_ goto. Like all powerful tools, it's easy to misuse, but used correctly, it is immensely powerful, and yes, I even miss it in Java sometimes. – fge Jan 28 '15 at 09:14
0

Use while loop with continue statement.

if(disk.equals("1") && tower.equals("b")) {
    slot[0] = slot[8];
continue;
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Afroz Shaikh
  • 362
  • 4
  • 18
0

As fge notes, goto is a reserved word in Java, but is not implemented. In most languages, it is generally considered bad programming practice to use goto. You should use a loop. An infinite while loop with a break-out condition should be sufficient:

while (true) {
    System.out.print('\u000C');
    System.out.println("  A  ,  B  ,  C  ");
    System.out.println("| " +slot[0]+ "  |  " +slot [1]+  "  |  " +slot[2]+ "  |");
    System.out.println("| " +slot[3]+ "  |  " +slot [4]+  "  |  " +slot[5]+ "  |");
    System.out.println("| " +slot[6]+ "  |  " +slot [7]+  "  |  " +slot[8]+ "  |");

    System.out.println("What number do you want to move <1, 2, 3, e to exit>: ");
    disk = (input.readLine());
    if (disk.equals("e")) {
        break;
    }

    System.out.println("What tower do you want to put <A, B, C>: ");
    tower = (input.readLine());

    // First Move 
    if (disk.equals("1") && tower.equals("b")) {
        slot[0] = slot[8];
    }
}
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
0
while (condition) {

  top:

  System.out.print('\u000C');
  System.out.println("  A  ,  B  ,  C  ");
  System.out.println("| " +slot[0]+ "  |  " +slot [1]+  "  |  " +slot[2]+ "  |");
  System.out.println("| " +slot[3]+ "  |  " +slot [4]+  "  |  " +slot[5]+ "  |");
  System.out.println("| " +slot[6]+ "  |  " +slot [7]+  "  |  " +slot[8]+ "  |");

  middle:

  System.out.println("What number do you want to move <1, 2, 3>: ");
  disk = (input.readLine());
  System.out.println("What tower do you want to put <A, B, C>: ");
  tower = (input.readLine());

  // First Move 
  if(disk.equals("1") && tower.equals("b")) {
    slot[0] = slot[8];
    // use continue skip the rest code
    // continue top;
    // if you want goto top, continue and continue top is same
    continue;
    // if you want goto middle, you can use the label
    // continue middle;
  }

  // some other code
}

You can proper use the label just in loop, but not goto, such as above code

aWang
  • 78
  • 6