基本上你不知道如何再次生成随机数.只需使用下面的内容
while(true) { //generate random number here if (can place battleship) { place battleship break //placed battleship so no need to retry. stop retrying } //else we will loop till we can place the battleship }