繁体   English   中英

Java While循环输入验证

[英]Java While Loop input validation

嗨,我想知道如何对if语句进行验证,这样程序只有在用户输入命令“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");
        }
    }   





}

}

在这种情况下,为if语句添加额外条件,例如,您可以说

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

 //do necessary actions
}

对于move命令,数组大小不应小于或大于3。

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

您可能有错误的参数数量或无效的参数。 用它来抓住所有人:

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

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

替换这个

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

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

您可以使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM