简体   繁体   English

Java中的一次循环?

[英]One Time Loop in java?

I have tried making a loop in java that only runs once, 我试过在Java中执行一次仅执行一次的循环,

package com.notelek.programs.flat;

import java.awt.event.KeyEvent;

import com.notelek.programs.flat.input.Controller;

public class Game {
    public static int time;
    public Controller controls;

    public Game(){
        controls = new Controller();
    }

    public void tick(Boolean[] key){
        time++;
        Boolean forward = key[KeyEvent.VK_W];
        Boolean back = key[KeyEvent.VK_S];
        Boolean left = key[KeyEvent.VK_A];
        Boolean right = key[KeyEvent.VK_D];
        Boolean turnLeft = key[KeyEvent.VK_LEFT];
        Boolean turnRight = key[KeyEvent.VK_RIGHT];
        for(Boolean i = true; i == true; i = false){
            forward = true;
            back = true;
            left = true;
            right = true;
            turnLeft = true;
            turnRight = true;
        }
        controls.tick(forward,back,left,right,turnLeft,turnRight);
    }    
}

That is my code, I need to set the forward backward left right turnleft turnright variables all to true, and then back to false quickly (within a second) Does anyone know a simple way of doing this? 那就是我的代码,我需要将向前向后向左向右向右向左向右向右的变量全部设置为true,然后迅速(在几秒钟之内)将其设置为false。有人知道这样做的简单方法吗?

How quickly are we talking, here? 我们在这里说话有多快? If you want to switch it within a second (much, much faster than a second), just do: 如果要在一秒内切换(比一秒快得多),请执行以下操作:

forward = true;
back = true;
left = true;
right = true;
turnLeft = true;
turnRight = true;
forward = false;
back = false;
left = false;
right = false;
turnLeft = false;
turnRight = false;

If you're looking for a toggle that you can manipulate, I would make your own method: 如果您正在寻找可以操纵的切换开关,那么我将使用您自己的方法:

public void toggle() {
    forward = !forward;
    back = !back;
    // and so on
}

... then invoke it after a certain amount of milliseconds have passed. ...然后在经过一定的毫秒数后调用它。

In the spirit of answering the question asked, though, if you really, really, really want to use a loop that only runs once, for some reason... here's the syntax for it: 但是,本着回答所问问题的精神,如果您确实真的想要使用仅运行一次的循环,由于某种原因……这是它的语法:

for(int i = 0; i < 1; i++) {
    // do stuff
}

If you really want a loop, use this: 如果您确实想要循环,请使用以下命令:

do {
    your code here
} while (false);

IMO, this is a bit silly... IMO,这有点愚蠢...

Why do you want to use a loop? 为什么要使用循环? If all you want to do is make something wait a second, why not use a wait command: 如果您只想稍等片刻,为什么不使用wait命令:

Thread.sleep(int milliseconds) Thread.sleep(以毫秒为单位)

You can just execute the block of code you want, wait a prescribed number of milliseconds and then execute another block of code: 您可以只执行所需的代码块,等待规定的毫秒数,然后执行另一个代码块:

int milliseconds = 1000;

forward = true;
back = true;
left = true;
right = true;
turnLeft = true;
turnRight = true;
Thread.sleep(milliseconds)
forward = false;
back = false;
left = false;
right = false;
turnLeft = false;
turnRight = false;

Possibly try something like this: 可能尝试这样的事情:

 for(int i = 0; i < 2; i++) { Boolean state = (i == 0); forwards = state; back = state; left = state; right = state; turnLeft = state; turnRight = state; // Optional sleep int milliseconds = 100; Thread.sleep(milliseconds); } 

The loop will go round once, set all to true (i == 0) then on the next repetion will set all to fasle , then quit. 循环将运行一次,将所有设置为true (i == 0),然后在下一次重复时将所有设置为fasle ,然后退出。

The Thread.sleep() is optional depending if you need the values to stay what they are for a short period. Thread.sleep()是可选的,具体取决于您是否需要这些值在短时间内保持原样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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