1

嗨,我想知道如何在 if 语句的 while 上进行验证,这样程序只有在用户输入命令“move”“line”“circle”及其参数时才会执行。例如,如果用户输入“move 200”,程序将显示 Invalid,因为只有一个或 NO 参数。谢谢!

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





}

}
4

5 回答 5

2

In the case , add extra condition for if statement, for eg, you can say

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

 //do necessary actions
}

for move command, the array size should not be less than or greater than 3.

Add conditions for other if statements according to the parameters for each command.

于 2012-11-09T09:51:30.827 回答
1

You could have either the wrong number of arguments or invalid arguments. Use this to catch 'em all:

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;
    }
}
于 2012-11-09T09:53:05.683 回答
1

如果将所有这些请求包装在 Command 对象中会怎样?我认为 Command 设计模式非常适合您的情况。命令模式说明

于 2012-11-09T10:00:42.913 回答
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;
    }
}

并将其应用于您的其他命令。

于 2012-11-09T09:47:42.553 回答
0

您可以使用:

if (splitUpText.lenght() != 3)
    System.err.println("Invalid...");
于 2012-11-09T09:49:36.673 回答