javaeclipseroboticsfinch

Java: duration of movement of Finch Robot


I'm new to programming. I have code for my Finch robot that simply loops a zigzag section after the user inputs how many times it should loop for, but how do I input another question that asks how long each zigzag section should be?

For example, the first question I ask is how many zigzag sections the user wants to loop for, but I also want to ask how long each zigzag segment should be (how long each line should be before it turns the other way).

Code:

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);

Solution

  • Check java Scanner doccumentation here

    Code sample to accept multiple inputs

    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);
       }
    }