为什么这会在收到值之前终止?(Java)的

Dav*_*vid 2 java methods while-loop

这是相关的代码段.

public static Territory[] assignTerri (Territory[] board, String[] colors) 
{ 
    for (int i = 0; i<board.length; i++) 
    { 
        // so a problem is that Territory.translate is void fix this. 
        System.out.print ("What team controls ") ; Territory.translate (i) ; System.out.println (" ?") ; 

        boolean a = false ;
        while (a = false) 
        {
            String s = getIns () ;
            if ((checkColor (s, colors)))
            {
                board[i].team = (returnIndex (s, colors)) ;
                a =true ; 
            }
            else 
                System.out.println ("error try again") ; 
        }       
        System.out.print ("How many unites are on ") ; Territory.translate (i) ; System.out.println (" ?") ; 
        int n = getInt () ; 
        board[i].population = n ; 
    }
    return board ; 

}
Run Code Online (Sandbox Code Playgroud)

作为附加信息,checkColor只需检查以确保其第一个参数(字符串)是其第二个参数(数组)的索引之一中的字符串.

在我看来,当while方法从键盘获取一个字符串,然后只有当该字符串检出是真的并且while允许终止.

我得到的输出是这样的:

What team controls Alaska ?
How many unites are on Alaska ?
Run Code Online (Sandbox Code Playgroud)

(最后有空格输入输入)

这似乎表明while输入之前的终止是因为第一行文本在while第二行文本之后而在它之外.

为什么会这样?

mob*_*mob 11

因为你搞砸===

  • 这就是为什么我总是使用约定const == var而不是var == const.那一天我忘了一个=编译器将拒绝编译,无论langauge和typ (2认同)

Mic*_*odd 6

因为你需要使用

while (a == false)
Run Code Online (Sandbox Code Playgroud)

要么

while (!a)
Run Code Online (Sandbox Code Playgroud)

代替.

('='是赋值运算符.'=='是比较运算符.在这种情况下,您需要使用比较运算符.)