简体   繁体   中英

How to use For loops and While loops to repeat code block

I need the make the 2nd if statement (when a button is pressed) repeat the command 5 times using a for loop, then a while loop for the 3rd if statement

The while and for loops are the things Ive tried but I have not gotten the code to work. It only draws one dot at a random location.

    if (mouseX >= BUTTONX && mouseX <= BUTTONX+BUTTONWIDTH && mouseY >= BUTTONY && mouseY <= BUTTONY+BUTTONHEIGHT) {
        //if click on button, choose random position
        x = (int)random(width);
        y = (int)random(height);
    } else {
        if (mouseX >= BUTTONX && mouseX <= BUTTONX+BUTTONWIDTH && mouseY >= BUTTONY2 && mouseY <= BUTTONY2+BUTTONHEIGHT) {
            for(int a = 0; a <= 5; a++){
                x = (int)random(width);
                y = (int)random(height);
            }
        } else {
            if (mouseX >= BUTTONX && mouseX <= BUTTONX+BUTTONWIDTH && mouseY >= BUTTONY3 && mouseY <= BUTTONY3+BUTTONHEIGHT) {
                int i = 0;
                while(i <= 5){
                    x = (int)random(width);
                    y = (int)random(height);
                    i++;
                }
            } else {
                //if click off button, place dot at mouse location
                x = mouseX;
                y = mouseY;
            }
        }
    }
}

You are computing several values for x and y but you are only going to use the final (5th) values since there is nothing in the loop to use them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM