【问题标题】:Throw exception in switch statement在 switch 语句中抛出异常
【发布时间】:2013-11-26 12:10:16
【问题描述】:

这个程序是我们在音乐商店中可以找到的简化程序。我们输入我们想要的命令,它会执行该命令应该做的事情。这些是选项。

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

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

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

程序必须执行,直到我们输入“close”。

我在这里发布我到目前为止所做的事情。

主要问题是,如果我们输入了错误的命令或输入了此处不存在的内容,如何抛出异常。现在,如果发生这种情况,它就会回到开始。如果您就您认为可能对程序更好的任何更改或修改给我建议,我也会很感激,我只是从这个开始,我知道这可以以更好的方式完成:

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的使用

因为这个原因,我不能使用默认值。在开关里面我有 createlist 命令,但我必须在女巫之外设置 erase 命令,因为这些取决于我是否输入带有代码的命令,或者没有它。所以,如果我输入一个擦除命令,并且如果我在开关中有默认值,它将显示异常。

【问题讨论】:

  • 在switch中,使用默认:\\抛出异常。
  • 要在 switch 语句中抛出异常,您将抛出异常。唯一令人意外的是,如果 case 只抛出异常——没有if 逻辑——那么如果你使用它,break 将会死掉并且编译器会抱怨。所以跳过break
  • 发布关于使用默认命令的更新

标签: java exception switch-statement


【解决方案1】:

如果您想使用switch/case 添加default 来处理未知命令。你不需要任何例外。只需正确处理错误的输入。 因此,您必须预先解析输入并确定这些是否是具有三个参数的情况。将特殊情况的代码移到switch/case 中并检查第三个参数。我为一个案例做的,这样你就可以知道了。 所以默认情况下,有点脏,未测试代码将是:

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

【讨论】:

  • 我现在试试,只有一件事……在这一行preparsedInstructions = ss1.substring(0, to);ss1 是什么变量?
【解决方案2】:

只是为了简化

        case "list selling":
                //listSelling();
                break;

        default:
            if(instructions.equals("erase music") || instructions.equals("erase client") ){
                System.out.println("hello delete");
                //do your operation
            }
            else{
                System.out.println("hello default throw exceliotn");
                throw new RuntimeException("Invalid entry");

            }
        }

您可以有效地将if 子句移入方法中,并且可以返回“布尔标志”来确定是否抛出异常。这将增加可读性。

【讨论】:

    【解决方案3】:

    您可以添加一个default: 案例,在该案例中您处理正在输入的其他内容。如果值不是任何列出的选项,则执行default: 中的语句。我相信switch's documentation 很好地展示了如何做到这一点。

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2016-12-15
      相关资源
      最近更新 更多