0

我是编程新手。我有我的 Finch 机器人的代码,它在用户输入它应该循环多少次后简单地循环一个锯齿形部分,但是我如何输入另一个问题,询问每个锯齿形部分应该多长时间?

例如,我问的第一个问题是用户想要循环多少个 zigzag 部分,但我还想问每个 zigzag 段应该多长(每条线在转向另一个方向之前应该多长)。

代码:

Finch myFinch = new Finch();
Scanner sc = new Scanner(System. in );

System.out.println("Welcome! Complete the following entries");
System.out.println("Number of zigzag sections:  ");

int noOfTimes = sc.nextInt();

do {
    myFinch.setLED(Color.green);
    myFinch.buzz(600, 2250);
    myFinch.setWheelVelocities(180, 0, 750);
    myFinch.setWheelVelocities(100, 100, 1500);
    myFinch.setLED(Color.red);
    myFinch.buzz(600, 2350);
    myFinch.setWheelVelocities(0, 180, 850);
    myFinch.setWheelVelocities(180, 180, 1500);
    noOfTimes--;

} while ( noOfTimes > 0 );

myFinch.quit();
System.exit(0);
4

1 回答 1

0

在此处检查 javaScanner文档

接受多个输入的代码示例

import java.util.Scanner;

class GetInputFromUser
{
   public static void main(String args[])
   {
      int a;
      float b;
      String s;

      Scanner in = new Scanner(System.in);

      System.out.println("Enter an integer");
      a = in.nextInt();
      System.out.println("You entered integer " + a);

      System.out.println("Enter a float");
      b = in.nextFloat();
      System.out.println("You entered float " + b);  

      System.out.println("Enter a string");
      s = in.nextLine();
      System.out.println("You entered string " + s);
   }
}
于 2019-03-01T20:25:10.230 回答