Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 防止递增_Java - Fatal编程技术网

Java 防止递增

Java 防止递增,java,Java,我手头有一件小事。所以我试图阻止我的计数器c增加for循环。我正试图填补池塘里的一块空地,除非它是空的。如果它已经装满了另一条鱼(白色或红色),我不希望计数器增加。一旦池中的一个点(或者更确切地说是元素)被填充,就不能再填充了。所以到最后应该有500条白鱼和5条红鱼 我觉得我在用错误的条件语句来处理这个问题。一旦我的计数器递增,调用方法placeFish的while语句也会递增白色或红色计数器,这不是我想要做的。我得到的白色/红色鱼的总数不是500或5,而是更低,因为while计数器在增加,而理

我手头有一件小事。所以我试图阻止我的计数器c增加for循环。我正试图填补池塘里的一块空地,除非它是空的。如果它已经装满了另一条鱼(白色或红色),我不希望计数器增加。一旦池中的一个点(或者更确切地说是元素)被填充,就不能再填充了。所以到最后应该有500条白鱼和5条红鱼

我觉得我在用错误的条件语句来处理这个问题。一旦我的计数器递增,调用方法placeFish的while语句也会递增白色或红色计数器,这不是我想要做的。我得到的白色/红色鱼的总数不是500或5,而是更低,因为while计数器在增加,而理想情况下我不希望它们增加

我使用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?
        }
    }
}
publicstaticvoidfishes(int[][]池塘){
//pond有一个称为fishes的不同方法中的维度[50][50]
//二维阵列池中的每个元素都已设置为值0
int白鱼=500;
int红鱼=5;
int whitefishvalue=1
int redfishvalue=2
int-white=0;
红色整数=0;
while(白色<白鱼)
{
砂鱼(池塘、白鱼价值);
白色++;
}
而(红色<红鱼)
{
placeFish(池塘红鱼价值);
redd++;
}
}
公共静态空置鱼(int[][]池塘,int新鱼){
int a=随机的.nextInt(池塘长度);
int b=random.nextInt(池塘[0].长度);
int spot=0;
对于(int c=0;c<1;c++)
{
if(池塘[a][b]==现场)
{
池塘[a][b]=新鱼;
C++;
/如何停止C++的递增?
}
}
}

我不太确定您想做什么,但我认为这正是您想要的。。。这将在数组中随机搜索一个点,当你找到一个点时,它将停止,然后将鱼放在那里

public static void placeFish(int[][] pond, int newFish) {
    int spot = 0;
    int a;
    int b;

    do
    {
        a = random.nextInt(pond.length);
        b = random.nextInt(pond[0].length);
    } while (pond [a][b] != spot);

    pond[a][b] = newFish;
}
只相当于
while
循环

initialize;
while (condition) {
    // stuff goes here
    increment;
}
因此,在循环的每次迭代结束时,它会自动递增
c

增加
c
的另一个位置在
if
语句的主体中。只有当池塘[a][b]==spot时才会发生这种情况。因此,在这是真的迭代中,您总共增加
c
两次,一次在这个
if
语句中,一次在循环结束时

我猜你只想在
pond[a][b]==spot
时增加一次,否则就不会增加,对吧?如果是这样,这是一个简单的修复方法:只需删除在每个循环迭代结束时运行的递增语句

for (int c = 0; c < 1;) {
    // stuff goes here
}
for(int c=0;c<1;){
//这里有东西
}
这样,在
if
语句中只剩下一行增量



顺便说一句,请注意,使用只有一次迭代的
for
循环是没有意义的。

您的措辞非常混乱,但我假设您不希望for循环每次都递增

for (int c = 0; c < 1;) {
    // stuff goes here
}
for (int c = 0; c < 1;) //It's not necessary to put an increment there.  You can in fact write a loop like for(;;) and escaping it via break
{
    if (pond [a][b] == spot)
    {
        pond[a][b] = newFish;
        c++;
                //How to stop c++ from incrementing?
    }
}
for(int c=0;c<1;)//不需要在那里添加增量。实际上,您可以编写一个类似于(;;)的循环,并通过break对其进行转义
{
if(池塘[a][b]==现场)
{
池塘[a][b]=新鱼;
C++;
/如何停止C++的递增?
}
}

如果您不想以这种方式通过循环增加值,这似乎有点不对劲。你能解释一下你想完成什么吗?对不起,我实际上是在从内存中写代码,因为我现在没有笔记本电脑。所以我正在手工完成我的作业,然后将作业复制到eclipse上。非常感谢!这正是我想要的:)@Sozziko:很高兴我能帮上忙。请记住,不要因为上次代码成功就编写代码,先计划好要做的事情,然后再编写。您的问题是关于“防止递增”,但实际上根本不需要计数器。祝你好运
for (int c = 0; c < 1;) //It's not necessary to put an increment there.  You can in fact write a loop like for(;;) and escaping it via break
{
    if (pond [a][b] == spot)
    {
        pond[a][b] = newFish;
        c++;
                //How to stop c++ from incrementing?
    }
}