在switch语句中引发异常

阳刚之气

这个程序比我们在音乐商店中找到的程序要简化。我们输入我们想要的命令,它将使该命令能够完成。这些是选项。

  • 创建“客户” |“音乐” |“销售

  • 列出“客户” |“音乐” |“销售

  • 擦除“客户端” |“音乐” |“销售”(使用或不使用我们创建新语句时生成的代码)

该程序必须执行,直到我们键入“关闭”。

在这里,我将发布到目前为止的操作。

主要问题是,如果我们键入错误的命令或键入此处不存在的内容,该如何引发异常。现在,这种情况发生了,它可以追溯到开始。如果您对我认为可能对程序更好的任何更改或修改提供建议,我也将不胜感激,我只是从这一点开始,我知道这可以通过更好的方式来完成:

public class Main {

    static GestionarMusica musiclist= new GestionarMusica(20);
    static GestionarCliente clientlist= new GestionarCliente(20);

    static Scanner input= new Scanner(System.in);
    static String instructions;

    public static void main (String[] args){

    do{
        try{
            System.out.println("Waiting for instructions: ");
            instructions= input.nextLine();

            switch (instructions){
                case "create client":
                    createClient();
                    break;
                case "create music":
                    createMusic();
                    break;
                case "create selling":
                    //createSelling();
                    break;

                case "list client":
                    listClient();
                    break;
                case "list music":
                    listMusic();
                    break;
                case "list selling":
                    //listSelling();
                    break;
            }
        }catch (NullPointerException npe){
            System.out.println("There are not articles on the list");
        }

        if (instructions.equals("erase client")){
            eraseClientWithoutCode();
        }
        if (instructions.length() <= 18 && instructions.length() >= 17 && instructions.substring(0, 16).equals("erase client")){
            String client_code = instructions.substring(16);
            client_code = client_code.trim();
            int code = Integer.parseInt(client_code);
            eraseClientWithCode(code);
        }

        if (instruction.equals("erase music")){
            eraseMusicWithoutCode();
        }
        if (instructions.length() <= 17 && instructions.length() >= 16 && instructions.substring(0, 15).equals("erase music")){
            String music_code = instructions.substring(15);
            music_code = music_code.trim();
            int code = Integer.parseInt(music_code);
            eraseMusicWithCode(code);
        }

        if (instructions.equals("erase selling")){
            eraseSellingWithoutCode();
        }
        if (instructions.length() <= 16 && instructions.length() >= 15 && instructions.substring(0, 14).equals("erase selling")){
            String selling_code = instructions.substring(14);
            selling_code = selling_code.trim();
            int code = Integer.parseInt(selling_code);
            eraseSellingWithCode(code);
        }

    }while(!instructions.equals("close"));
}


public static void createMusic() {
    System.out.println("Insert album title: ");
    String title = teclado.nextLine();
    System.out.println("Insert album autor: ");
    String autor = teclado.nextLine();
    System.out.println("Insert format: ");
    String format = input.nextLine();

    musiclist.add(new Music(title, autor, format, musiclist.generateCode()));
}

public static void listMusic() {
    System.out.println(musiclist.toString());
}

public static void eraseMusicWithCode(int code) {
    musiclist.delete(code);
    System.out.println("Article deleted");
}

public static void eraseMusicWithoutCode() {
    System.out.println("Insert article code: ");
    int code = input.nextInt();
    musiclist.delete(code);
    System.out.println("Article deleted");
}

UPDATE-关于在switch语句中使用default

由于这个原因,我无法使用默认值。在交换机内部,我具有创建列出命令,但是我不得不在巫婆外部设置擦除命令,因为这些取决于我是否输入带有代码的命令,也可以不输入代码。因此,如果我输入了擦除命令,并且开关中具有默认值,它将显示异常。

用户名

如果要使用switch/caseadddefault处理未知命令。您不需要任何例外。只需正确处理错误的输入即可。因此,您将必须准备输入并确定这些输入是否为带有三个参数的情况。将特殊情况的代码移入内部switch/case并检查第3个参数。我做到了一种情况,这样您就可以了解想法。因此,默认情况下,有些脏的,未经测试的代码将是:

    public static void main (String[] args){

    do{
        try{
            System.out.println("Waiting for instructions: ");
            instructions= input.nextLine();
        String preparsedInstructions = instructions;
        int from = instructions.indexOf(" ");
        if(from > -1){
            int to = preparsedInstructions.indexOf(" ", from + 1);
            if(to > -1){
                preparsedInstructions = preparsedInstructions.substring(0, to);
            }
        }

            switch (preparsedInstructions){
                case "create client":
                    createClient();
                    break;
                case "create music":
                    createMusic();
                    break;
                case "create selling":
                    //createSelling();
                    break;

                case "list client":
                    listClient();
                    break;
                case "list music":
                    listMusic();
                    break;
                case "list selling":
                    //listSelling();
                    break;
                case "erase client":
            if (instructions.length() <= 18 && instructions.length() >= 17 && instructions.substring(0, 16).equals("erase client")){
                String client_code = instructions.substring(16);
                client_code = client_code.trim();
                int code = Integer.parseInt(client_code);
                eraseClientWithCode(code);
            }else{
                    eraseClientWithoutCode();
            }
                    break;
        ...//do for erase music,selling

        default: //error handling
            }
        }catch (NullPointerException npe){
            System.out.println("There are not articles on the list");
        }



    }while(!instructions.equals("close"));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

模拟枚举的静态方法并在switch语句中使用该枚举对象时,PowerMockito引发异常

来自分类Dev

模拟枚举的静态方法并在switch语句中使用该枚举对象时,PowerMockito引发异常

来自分类Dev

switch语句中的Switch语句

来自分类Dev

C代码中if&switch语句中的异常输出

来自分类Dev

C代码中if&switch语句中的异常输出

来自分类Dev

在Dispatcher.Invoke语句中引发异常时,Dispatchers UnhandledException处理程序未处理异常

来自分类Dev

要在Python的try语句中检查的if语句,还需要执行代码以防引发异常

来自分类Dev

要在Python的try语句中检查的if语句,还需要执行代码以防引发异常

来自分类Dev

if语句中的异常

来自分类Dev

switch语句中的Hasvalue

来自分类Dev

Switch语句中的错误

来自分类Dev

如何在Rails rescue_from语句中重新引发Ruby异常?

来自分类Dev

node.js承诺:如何找出哪个迭代在.catch语句中引发了异常?

来自分类Dev

如何在Rails rescue_from语句中重新引发Ruby异常?

来自分类Dev

C#抛出一个异常后没有引发异常的try语句中的代码会发生什么

来自分类Dev

'if'语句中的空指针异常

来自分类Dev

python打印语句中的异常

来自分类Dev

在Switch语句中替换Char

来自分类Dev

在switch语句中调用类?

来自分类Dev

switch语句中的独立中断

来自分类Dev

Switch语句中的多个参数

来自分类Dev

在switch语句中调用函数。

来自分类Dev

在Switch语句中声明变量

来自分类Dev

SWITCH CASE语句中的MySQL IF

来自分类Dev

在 switch 语句中捕获组

来自分类Dev

switch 语句中的绑定参数

来自分类Dev

在if语句中执行的switch语句未执行

来自分类Dev

玩笑覆盖 - switch 语句中的 if 语句

来自分类Dev

在Python中的if语句中处理异常

Related 相关文章

  1. 1

    模拟枚举的静态方法并在switch语句中使用该枚举对象时,PowerMockito引发异常

  2. 2

    模拟枚举的静态方法并在switch语句中使用该枚举对象时,PowerMockito引发异常

  3. 3

    switch语句中的Switch语句

  4. 4

    C代码中if&switch语句中的异常输出

  5. 5

    C代码中if&switch语句中的异常输出

  6. 6

    在Dispatcher.Invoke语句中引发异常时,Dispatchers UnhandledException处理程序未处理异常

  7. 7

    要在Python的try语句中检查的if语句,还需要执行代码以防引发异常

  8. 8

    要在Python的try语句中检查的if语句,还需要执行代码以防引发异常

  9. 9

    if语句中的异常

  10. 10

    switch语句中的Hasvalue

  11. 11

    Switch语句中的错误

  12. 12

    如何在Rails rescue_from语句中重新引发Ruby异常?

  13. 13

    node.js承诺:如何找出哪个迭代在.catch语句中引发了异常?

  14. 14

    如何在Rails rescue_from语句中重新引发Ruby异常?

  15. 15

    C#抛出一个异常后没有引发异常的try语句中的代码会发生什么

  16. 16

    'if'语句中的空指针异常

  17. 17

    python打印语句中的异常

  18. 18

    在Switch语句中替换Char

  19. 19

    在switch语句中调用类?

  20. 20

    switch语句中的独立中断

  21. 21

    Switch语句中的多个参数

  22. 22

    在switch语句中调用函数。

  23. 23

    在Switch语句中声明变量

  24. 24

    SWITCH CASE语句中的MySQL IF

  25. 25

    在 switch 语句中捕获组

  26. 26

    switch 语句中的绑定参数

  27. 27

    在if语句中执行的switch语句未执行

  28. 28

    玩笑覆盖 - switch 语句中的 if 语句

  29. 29

    在Python中的if语句中处理异常

热门标签

归档