1

こんにちは、ifステートメントのwhileを検証して、ユーザーがパラメータを使用してコマンド「move」「line」「circle」を入力した場合にのみプログラムが実行されるようにするにはどうすればよいでしょうか。たとえば、ユーザーが「move 200」と入力すると、パラメータが1つしかないか、パラメータがないため、プログラムは無効と表示します。ありがとう!

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

この場合、ifステートメントの条件を追加します。たとえば、次のように言うことができます。

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

 //do necessary actions
}

moveコマンドの場合、配列サイズは3以上にする必要があります。

各コマンドのパラメーターに従って、他のifステートメントの条件を追加します。

于 2012-11-09T09:51:30.827 に答える
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;
    }
}
于 2012-11-09T09:53:05.683 に答える
1

これらすべてのリクエストをコマンドオブジェクトでラップするとどうなりますか?コマンドデザインパターンはあなたのケースによく合っていると思います。コマンドパターンの説明

于 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 に答える