簡體   English   中英

沒有退出“while”循環,但為什么呢?

[英]not exiting out of “while” loop, but why?

所以我在這里有一個簡單模仿跳棋的程序(但不是跳躍,一件只是“吃”另一件)。 我有一個實現“Checkers”類對象的簡單類,並創建一個名為“chips”的int數組變量,有一個“WHILE”語句創建一個循環,而chip [0]和chips [1]整數大於0。它看起來像這樣:

public static void main(String [] args){

        Checkers c = new Checkers();
        c.init();
        c.printBoard();
        int[] chips = c.count();
        Scanner kbd = new Scanner(System.in);
        while(chips[0]>0 && chips[1]>0){
            System.out.println("\nYour move? 4 ints: src row, src col, dest row, dest col separated by [SPACE]");
            int srcR = kbd.nextInt();
            int srcC = kbd.nextInt();
            int destR = kbd.nextInt();
            int destC = kbd.nextInt();
            kbd.nextLine();
            c.move(srcR,srcC,destR,destC);
            c.printBoard();
            System.out.println(c.getMessage());
            c.count();
        }
    }

我在Checkers類中的count方法如下所示:

public int[] count() {  

    int wht=0;
    int blk=0;

    System.out.println();

    for(int i=0; i<board.length;i++)    {
        for(int z=0; z<board[0].length;z++)     {
            if(board[i][z] instanceof White)    {
                wht++;
            }
            else if(board[i][z] instanceof Black)   {
                blk++;
            }
        }
    }

    int[] arr = {wht,blk};  
    System.out.println("Whites: "+wht+"\nBlacks: "+blk);    


    return arr;   
}

電路板是8 x 8(這是board.length的),計數方法只是返回“arr”的值,以便它可以報告回“芯片”以決定芯片是否[1]和籌碼[0]都大於一個(他們在棋盤上都至少有一個白色或黑色棋子。)陣列只應該由兩個值組成(即。{white,black};)我試過以各種方式重新格式化整個count(),但這只是代碼編譯的幾種方式之一。

這是輸出:

01234567

0 | ........

1 | ..B .....

2 | ........

3 | ........

4 | ........

5 | ..BBB

6 | .BBBB

如你所見,不再有白色碎片(應該在頂部)。 我很感激幫助,因為這可能是一個簡單的解決方案,正好在我的鼻子下面,但它很煩人,所以我希望我解釋得這么好。 謝謝。

哦順便說一下,我被告知不要觸摸主要方法。

你正在丟棄你的循環中的籌碼數...改變線:

c.count();

成:

chips = c.count();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM