Java 我需要帮助添加一个循环来重复我的代码四次
下面是我正在使用的代码。我是编程新手,我知道我需要使用一个循环来设置速度,我已经重复了四次,我只是不知道怎么做。任何帮助都将不胜感激Java 我需要帮助添加一个循环来重复我的代码四次,java,eclipse,Java,Eclipse,下面是我正在使用的代码。我是编程新手,我知道我需要使用一个循环来设置速度,我已经重复了四次,我只是不知道怎么做。任何帮助都将不胜感激 package Code.simpleOutput; import edu.cmu.ri.createlab.terk.robot.finch.Finch; public class GeoPattern { public static void main(final String[] args) { Fin
package Code.simpleOutput;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class GeoPattern {
public static void main(final String[] args)
{
Finch myFinch = new Finch();
myFinch.setWheelVelocities(255,255,1000);
myFinch.setWheelVelocities(255,0,800);
myFinch.setWheelVelocities(255,255,1000);
myFinch.setWheelVelocities(255,0,800);
myFinch.setWheelVelocities(255,255,1000);
myFinch.setWheelVelocities(255,0,800);
myFinch.setWheelVelocities(255,225,1000);
myFinch.quit();
System.exit(0);
}
}
你只是在找我吗
for (int i = 0; i < 4; i++) {
myFinch.setWheelVelocities(255,255,1000);
myFinch.setWheelVelocities(255,0,800);
}
您可以使用for循环
for(int i = 0; i < 4; i++)
{
//code to repeat goes here
}
这是一个for循环
for(int x = 4; x >0; x--) {
myFinch.setWheelVelocities(255,255,1000);
myFinch.setWheelVelocities(255,0,800);
}
不过,你需要更多地解释你的目标
此代码将车轮速度设置为2552551000,然后立即将其更改回255,0800,并执行四次
你不是在尝试在它们之间切换吗,这需要某种暂停、定时器或测试吗
同时,您确实应该仔细阅读Oracle Java文档,尤其是控制结构:
如果您是Java新手,那么异常和线程可能会有点困难,但请耐心听我说:
Thread.sleep(4000);
此指令将使您的代码暂停4秒,参数为毫秒。
为了使用暂停代码,您需要在此处声明您的方法main:
为什么??因为,如果您的应用程序中有多个线程,那么另一个线程可能会在您的应用程序睡眠时中断。基本上,比如说,嘿,我需要计算一些东西,因为你睡着了,我要借用处理器,我稍后会还给你
因为你只有一个线程,你不需要麻烦,你只需要声明理论上它可能会发生,你就可以开始了
如果您希望切换速度并看到它发生,代码将变为:
package Code.simpleOutput;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class GeoPattern {
public static void main(final String[] args)
throws InterruptedException // because it contains a sleep call
{
Finch myFinch = new Finch();
for(int x = 4; x >0; x--)
{
Thread.sleep(1000); // sleep a second
myFinch.setWheelVelocities(255,255,1000);
Thread.sleep(1000); // sleep another second
myFinch.setWheelVelocities(255,0,800);
}
myFinch.quit();
System.exit(0);
}
}
应该阅读一些java tutorials=PHeh,对于相同的问题价格,OP得到一个向上循环和一个向下循环^^
package Code.simpleOutput;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class GeoPattern {
public static void main(final String[] args)
throws InterruptedException // because it contains a sleep call
{
Finch myFinch = new Finch();
for(int x = 4; x >0; x--)
{
Thread.sleep(1000); // sleep a second
myFinch.setWheelVelocities(255,255,1000);
Thread.sleep(1000); // sleep another second
myFinch.setWheelVelocities(255,0,800);
}
myFinch.quit();
System.exit(0);
}
}