我手边有一些微不足道的问题。所以我试图阻止我的计数器c增加for循环。只有在空的时候,我才会在池塘里填满一个点。如果它已经装满了另一条鱼(白色或红色),我不希望柜台增加。一旦池塘中的一个点(或元素)被填满,它就不能再被填满。所以最终应该有500只白色鱼和5只红色鱼。防止递增
我感觉好像我在用错误的条件语句来解决这个问题。一旦我的计数器增加,我的while语句也会调用方法placeFish,同样增加白色或红色计数器,这不是我想要做的。我总是得到总数不是500和5的白色/红色鱼,而是更低,因为当理想情况下,我不希望他们的时间计数器增加。
我正确使用for语句吗?我试过,但它似乎也没有工作。
public static void fishes (int[][] pond) {
//pond has dimensions [50][50] in a different method that call fishes
//every element in the 2D array pond is already set to value 0
int whitefish = 500;
int redfish= 5;
int whitefishvalue = 1
int redfishvalue = 2
int white = 0;
int red = 0;
while (white < whitefish)
{
placeFish (pond, whitefishvalue);
white++;
}
while (red < redfish)
{
placeFish (pond redfishvalue);
redd++;
}
}
public static void placeFish(int[][] pond, int newFish) {
int a = random.nextInt(pond.length);
int b = random.nextInt(pond[0].length);
int spot = 0;
for (int c = 0; c < 1; c++)
{
if (pond [a][b] == spot)
{
pond[a][b] = newFish;
c++;
//How to stop c++ from incrementing?
}
}
}
您似乎有点不喜欢以这种方式通过循环增加值。你能解释一下你想要完成什么吗? – Makoto