猿问

Finch Robot - 如何循环特定的代码行?(爪哇)

我对编码完全陌生,不清楚自己在做什么。我有一个 Finch 机器人的代码,它只是让它左右移动,但是我如何使这个简单的过程循环三遍呢?此外,是否可以实现一个代码来询问用户他们希望产品循环多少次?


抱歉,如果这似乎是一个愚蠢的问题。我到处看了看,不太明白如何正确循环代码。我要循环的代码发布在下面。


public static void main(final String[] args)

       {

          Finch myFinch = new Finch();


          myFinch.setLED(Color.green);

          myFinch.setWheelVelocities(180, 0, 750);

          myFinch.setWheelVelocities(100, 100, 1500);


          myFinch.setLED(Color.red);

          myFinch.setWheelVelocities(0, 180, 850);

          myFinch.setWheelVelocities(180, 180, 1500);


          myFinch.quit();

          System.exit(0);

          }            


慕盖茨4494581
浏览 90回答 2
2回答

杨魅力

第一种方法:使用 for 循环&nbsp;public static void main(final String[] args)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Finch myFinch = new Finch();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("How many times?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int noOfTimes = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int movement=0; movement < noOfTimes; movement++){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setLED(Color.green);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(180, 0, 750);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(100, 100, 1500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setLED(Color.red);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(0, 180, 850);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(180, 180, 1500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.quit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp;}第二种方法:使用while循环&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("How many times?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int noOfTimes = sc.nextInt();&nbsp; while(noOfTimes > 0){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setLED(Color.green);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(180, 0, 750);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(100, 100, 1500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setLED(Color.red);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(0, 180, 850);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(180, 180, 1500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; noOfTimes--;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}第三种方法:使用 do-while 循环&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("How many times?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int noOfTimes = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp;do{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setLED(Color.green);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(180, 0, 750);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(100, 100, 1500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setLED(Color.red);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(0, 180, 850);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myFinch.setWheelVelocities(180, 180, 1500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; noOfTimes--;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}while(noOfTimes > 0);

富国沪深

for循环:https ://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.htmlwhile循环:https ://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html读取用户输入:https ://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
随时随地看视频慕课网APP

相关分类

Java
我要回答