我正在做作业。它是河内任务的塔楼。 (我尚未添加大磁盘而不是小磁盘的代码)。当我将tower3设置为4、3、2、1时,它表示我赢了,但是当我在玩游戏时做到了,则什么也没有发生。请帮忙!这是美国东部时间星期一8:30 pm。

import java.util.Scanner;

/**
 * Simulates a tower that can hold disks.
 * @author S. Camilleri
 * @author <Hasan Zafar>
 */
public class Challenge {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // This array holds the disks. A 0 represents no disk.
        int[] tower = {4,3,2,1};
        int[] tower2 = {0,0,0,0};
        int[] tower3 = {0,0,0,0};

        // This index represents the first available empty spot for a disk.
        int index = 0;

        int towerCounter = 0;
        int length = tower.length;
        int length2 = tower2.length;
        int length3 = tower3.length;
        int diskChoice = 1;
        int i;
        int held = 0;
        int placeChoice;

        boolean playing = true;
        while (playing)
        {
            //Check if Won
            if (tower3[0] == 4 && tower3[1] == 3 && tower3[2] == 2 && tower[3] == 1) {
                System.out.println("Congratulations! You win!");
                playing = false;
                break;

            }

            /********************
             * Display the towers
             ********************/
            System.out.println();
            //Tower1
            System.out.print("{ ");

            for (int x=0; x<length; x++)
            {
                System.out.print(tower[x]);
            }

            System.out.println();
            //Tower2
            System.out.print("{ ");
            towerCounter = 0;

            for (int x=0; x<length2; x++)
            {
                System.out.print(tower2[x]);
            }

            System.out.println();
            //Tower3
            System.out.print("{ ");
            towerCounter = 0;

            for (int x=0; x<length3; x++)
            {
                System.out.print(tower3[x]);
            }



            /********************
             * Select the highest disk from the tower
             ********************/

            System.out.println();
            System.out.println("Pick a tower (The disk highest on that tower will be chosen)");
            diskChoice = input.nextInt();

            // If user uses the first tower
            if (diskChoice == 1) {
                i = 3;
                while (tower[i] == 0) {
                    i--;

                }
                held = tower[i];
                tower[i] = 0;
            } else if (diskChoice == 2) { // If user uses the second tower
                i = 3;
                while (tower2[i] == 0) {
                    i--;

                }
                held = tower2[i];
                tower2[i] = 0;
            } else if (diskChoice == 3) { // If user uses the third tower
                i = 3;
                while (tower3[i] == 0) {
                    i--;

                }
                held = tower3[i];
                tower3[i] = 0;
            }

            /********************
             * Place the disk
             ********************/

            System.out.println("Where would you like to place" + " " + held + "?");
            placeChoice = input.nextInt();

            if (placeChoice == 1) {
                i = 3;
                if (tower[3] == 0){
                    while (tower[i] == 0) {

                        i--;
                        if (i == 0) {
                            break;
                        }

                    }
                }

                if (tower[i] == 0) {
                    tower[i] = held;
                } else if (tower[i] != 0) {
                    tower[i+1] = held;
                }
            } else if (placeChoice == 2) {
                i = 3;

                if (tower2[3] == 0){
                    while (tower2[i] == 0) {

                        i--;
                        if (i == 0) {
                            break;
                        }

                    }
                }
                if (tower2[i] == 0) {
                    tower2[i] = held;
                } else if (tower2[i] != 0) {
                    tower2[i+1] = held;
                }
            } else if (placeChoice == 3) {
                i = 3;
                if (tower3[3] == 0){
                    while (tower3[i] == 0) {

                        i--;
                        if (i == 0) {
                            break;
                        }

                    }
                }
                if (tower3[i] == 0) {
                    tower3[i] = held;
                } else if (tower3[i] != 0) {
                    tower3[i+1] = held;
                }
            }



        }
    }
}


如果我手动将tower3设置为4321,则表示我赢了。
如果我在游戏中将tower3设置为4321,它将继续播放。

最佳答案

我想到了。我忘了在win语句中写3。

10-06 02:10