Finch Robot - 如何循环特定的代码行? (Java)
Finch Robot - How do I loop specific lines of code? (Java)
我对编码完全陌生,不清楚自己在做什么。我的 Finch 机器人有一个代码,可以简单地让它左右移动,但是我如何使这个简单的过程循环 3 次?另外,能否实现一个代码来询问用户他们希望产品循环多少次?
抱歉,如果这看起来像一个愚蠢的问题。我到处都看了,不太明白如何正确循环代码。我要循环的代码贴在下面。
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);
}
for 循环:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
while 循环:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
正在读取用户输入:https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
第一种方法:使用 for 循环
public static void main(final String[] args)
{
Finch myFinch = new Finch();
Scanner sc = new Scanner(System.in);
System.out.println("How many times?");
int noOfTimes = sc.nextInt();
for(int movement=0; movement < noOfTimes; movement++){
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);
}
第二种方法:使用 while 循环
Scanner sc = new Scanner(System.in);
System.out.println("How many times?");
int noOfTimes = sc.nextInt();
while(noOfTimes > 0){
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);
noOfTimes--;
}
第三种方法:使用do-while循环
Scanner sc = new Scanner(System.in);
System.out.println("How many times?");
int noOfTimes = sc.nextInt();
do{
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);
noOfTimes--;
}while(noOfTimes > 0);
我对编码完全陌生,不清楚自己在做什么。我的 Finch 机器人有一个代码,可以简单地让它左右移动,但是我如何使这个简单的过程循环 3 次?另外,能否实现一个代码来询问用户他们希望产品循环多少次?
抱歉,如果这看起来像一个愚蠢的问题。我到处都看了,不太明白如何正确循环代码。我要循环的代码贴在下面。
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);
}
for 循环:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
while 循环:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
正在读取用户输入:https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
第一种方法:使用 for 循环
public static void main(final String[] args)
{
Finch myFinch = new Finch();
Scanner sc = new Scanner(System.in);
System.out.println("How many times?");
int noOfTimes = sc.nextInt();
for(int movement=0; movement < noOfTimes; movement++){
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);
}
第二种方法:使用 while 循环
Scanner sc = new Scanner(System.in);
System.out.println("How many times?");
int noOfTimes = sc.nextInt();
while(noOfTimes > 0){
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);
noOfTimes--;
}
第三种方法:使用do-while循环
Scanner sc = new Scanner(System.in);
System.out.println("How many times?");
int noOfTimes = sc.nextInt();
do{
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);
noOfTimes--;
}while(noOfTimes > 0);