Java Random - 两个相等值中的一个如何不同于另一个?

Wil*_*run 0 java random int swing

Take a look at this code:

        while(oneRay[pick][0] == "" || oneRay[pick][0] == null)
        hold = oneRay.length;
        random = new Random(); 
        System.out.println(oneRay.length + "\n" + hold);
        pick = random.nextInt(hold);
        System.exit(0);
    }
Run Code Online (Sandbox Code Playgroud)

In the console output, I see 33 and then 0. How does that make any sense? First, I set hold equal to the length of oneRay, then I print the length of oneRay followed by hold, should I not see 33 and 33 or at worst 0 and 0? I know some of you have seen my many questions tonight but I cannot get past this loop, there is always some error or another, and in this case the Random wont proceed as the value of hold is 0... Please help me!

Lou*_*man 5

你用过

while(oneRay[pick][0] == "" || oneRay[pick][0] == null)
Run Code Online (Sandbox Code Playgroud)

然后你没有使用括号,这意味着你的代码实际上就像

while(oneRay[pick][0] == "" || oneRay[pick][0] == null) {
    hold = oneRay.length;
}
random = new Random(); 
System.out.println(oneRay.length + "\n" + hold);
Run Code Online (Sandbox Code Playgroud)

特别是,如果while条件不成立,则hold = oneRay.length甚至不执行.

(你也应该使用.equals("").isEmpty不是== "",你应该做的空校验第一而不是第二.)