2012-11-09 86 views
1

嗨我不知道我是否可以在if语句的时候进行验证,因此只有当用户使用它们的参数输入命令“移动”“线条”“圆圈”时,程序才会执行。例如,如果用户输入“移动200”,则程序会说因为只有一个或NO参数而无效。 谢谢!Java while循环输入验证

import java.util.Scanner; 

public class DrawingProgram1 { 

public static void main(String[] args) { 

    GraphicsScreen g = new GraphicsScreen(); 

    String store; 
    String command; 

    int move1; 
    int move2; 
    int line1; 
    int line2; 
    int circle; 

    while (true) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("Type 'help' for list of commands. Type 'end' to finish."); 
     System.out.println("Please enter a command:"); 
     store = scan.nextLine(); 
     String [] splitUpText = store.split(" "); 
     command = splitUpText[0]; 


     if (command.equalsIgnoreCase("move")) { 
      move1 = Integer.parseInt(splitUpText[1]); 
      move2 = Integer.parseInt(splitUpText[2]); 
      g.moveTo(move1, move2); 

     } 
     else if (command.equalsIgnoreCase("line")) { 
      line1 = Integer.parseInt(splitUpText[1]); 
      line2 = Integer.parseInt(splitUpText[2]); 
      g.lineTo(line1, line2); 
     } 
     else if (command.equalsIgnoreCase("circle")) { 
      circle = Integer.parseInt(splitUpText[1]); 
      g.circle(circle); 
     } 
     else if (command.equalsIgnoreCase("help")) { 
      System.out.println("Enter a command for move."); 
      System.out.println("Enter a command for line."); 
      System.out.println("Enter a command for circle."); 
     } 
     else if (command.equalsIgnoreCase("end")) { 
      System.exit(0); 
     } 
     else { 
      System.out.println("Error"); 
     } 
    } 





} 

} 
+0

您可能会强制用户使用其他输入格式,例如移动:“200” – Luke94

+0

只需检查'splitUpText'的长度。如果它不是3,则在显示消息后继续while循环。 –

回答

1

您可能有错误数量的参数或无效参数。用这个来抓住他们的全部:

if (command.equalsIgnoreCase("move")) { 
    try { 
     move1 = Integer.parseInt(splitUpText[1]); 
     move2 = Integer.parseInt(splitUpText[2]); 
     g.moveTo(move1, move2); 
    } catch (ArrayIndexOutOfBoundsException e1) { 
     Sytem.out.println("Please specify 2 parameters!") 
     continue; 
    } catch (NumberFormatException e2) { 
     Sytem.out.println("Invalid parameters!") 
     continue; 
    } 
} 
+0

相当丑陋 - 为什么不简单检查splitUpText的长度呢? – assylias

+1

,因为无论如何他需要为'NumberFormatException'尝试'try \ catch',这样他总是可以为他的命令添加参数并实现他们的动作,而不必记住更新'if'语句。 – jlordo

+1

非常感谢! – Parakis

0

替换此

if (command.equalsIgnoreCase("move")) { 
    move1 = Integer.parseInt(splitUpText[1]); 
    move2 = Integer.parseInt(splitUpText[2]); 
    g.moveTo(move1, move2); 
} 

蒙山

if (command.equalsIgnoreCase("move")) { 
    try { 
     move1 = Integer.parseInt(splitUpText[1]); 
     move2 = Integer.parseInt(splitUpText[2]); 
     g.moveTo(move1, move2); 
    } catch (ArrayIndexOutOfBoundsException e) { 
     Sytem.out.println("move needs 2 parameters!") 
     continue; 
    } 
} 

和应用给你的其他命令。

+0

相当丑陋 - 为什么不简单检查splitUpText的长度呢? – assylias

0

您可以使用:

if (splitUpText.lenght() != 3) 
    System.err.println("Invalid..."); 
2

在这种情况下,添加额外的条件if语句,用于例如,你可以说

if(command.equalsIgnoreCase("move") && splitUpText.length == 3){ 

//do necessary actions 
} 

的移动命令,数组的大小应不大于或等于3.

根据每个命令的参数为其他if语句添加条件。

+0

非常感谢! – Parakis

1

如果你把所有这些请求包装在Command对象中怎么办?我认为命令设计模式适合你的情况。 Command pattern explanation