2016-02-16 87 views
0

鑑於以下作爲控制檯輸入:掃描器控制檯輸入錯誤

F 6 100 

我希望6被分配給duration,並且100分配給speed

if(command.equalsIgnoreCase("F")) 
myf.setWheelVelocities(speed,speed,duration); 

上面的代碼但似乎並不奏效。完整的代碼如下:我如何正確地將控制檯輸入分配給Finch機器人的setWheelVelocities方法參數?

import edu.cmu.ri.createlab.terk.robot.finch.Finch; 
import java.util.Scanner; 
public class CS1810 { 

    public static void main(String args[]) 
    { 
     Scanner scan = new Scanner(System.in); 
     Finch myf = new Finch(); 

     // Welcome Message 
     System.out.println("Welcome to Finch Robot Remote Navigation!!!"); 
     System.out.println(); 

     // Instructions 
     System.out.println("Instructions on how to navigate and commands to be used:"); 
     System.out.println("To move forward type in 'F' followed by a space and then enter an integer representing 'duration' followed by a space and then enter an integer representing 'speed'."); 
     System.out.println("To move left or right type in 'L' or 'R' followed by a space and then enter an integer representing 'duration' followed by a space and then an integer representing 'speedLeft' followed by a space and then an integer representing 'speedRight'."); 
     System.out.println("To backtrack type in 'B' followed by a space and then enter an integer representing 'track' which is the number of commands you want to backtrack."); 
     System.out.println("To stop the program type in 'S'."); 
     System.out.println(); 

     // Rules 
     System.out.println("Rules for commands and values entered:"); 
     System.out.println("You can only enter 'F,L,R,B and S' as commands."); 
     System.out.println("You can only enter a 'duration' value which is less than or equal to 6."); 
     System.out.println("You can only enter a 'speed','speedLeft' and 'speedRight' value between -100 and 100."); 
     System.out.println("You can only enter a 'backtrack' value equal to or less than the number of commands you have entered before:"); 
     System.out.println(); 

     //Enter Command 
     System.out.println("Enter your command below:"); 
     String command = (""); 
     int duration = scan.nextInt(); 
     int speed = scan.nextInt(); 

     // Ignore Cases 
     while((command.equalsIgnoreCase("F"))||(command.equalsIgnoreCase("L"))||(command.equalsIgnoreCase("R"))||(command.equalsIgnoreCase("B"))||(command.equalsIgnoreCase("S"))) 
     { 
      if(command.equalsIgnoreCase("F")) 
      myf.setWheelVelocities(speed,speed,duration); 
      System.out.println("Enter a new command"); 
      command = scan.nextLine();  
     } 
     myf.quit(); 
    } 
} 
+0

是的,這是可以做到。只需將您的輸入輸入到一個字符串中,然後在空間上分割並分配給每個變量。 – tnw

+0

請問這個怎麼辦? – CodeJava

+0

請只粗略搜索一下Google如何輸入字符串並用java解析它。 – tnw

回答

-1

你有分裂,然後在提供這個環節是回答如何通過空格在字符串分割: How do I split a string with any whitespace chars as delimiters?

+0

這應該是一條評論。我知道你現在還不能評論,但這不是將評論添加爲答案的藉口。事實上,這個問題甚至可能是你所鏈接問題的重複。 – tnw

+0

是的,我還不能評論 – i3rd