为什么此Java switch语句默认调用一个方法而不是中断?编辑:为每种情况添加了中断,问题仍然存在

问题描述 投票:-5回答:2

Context

我有一个Java CLA,它内置于IntelliJ中,并由Gradle管理。用户在给定的菜单中输入数字以添加/更改/查看联系人,然后退出。

问题

switch语句默认中断存在问题。如果程序运行并且您选择了默认值,它将按预期中断。

但是,如果您先选择另一种情况然后尝试中断,它将首先调用displayContacts,并且当您返回菜单并再次中断时,它将引发以下情况:

对于任务':Main.main(),执行失败。处理'命令'/ Applications / IntelliJ IDEA CE.app/Contents/jbr/Contents/Home/bin/java”完成,退出值非零1

switch语句永远不要调用此方法;为什么会发生这种情况,以及导致此错误的原因是什么?

Main Class

import java.util.ArrayList;

public class Main {

    static Input input = new Input();
    static ArrayList<Contact> contactList = new ArrayList<Contact>();

    public static void main(String[] args) {
        showMenu();
    }

    public static void showMenu() {
        printMenuOptions();
        switch (input.menuChoice()) {
            case 1: {
                newContact();
                break;
            }
            case 2: {
                updateExistingContact();
                break;
            }
            case 3: {
                showContact();
                break;
            }
            default: break;
        }
    }

    static void printMenuOptions() {
        System.out.println("Welcome to Contact Manager");
        System.out.println("Please select an option:");
        System.out.println("1. Create new Contact");
        System.out.println("2. Update Contact");
        System.out.println("3. View Contact");
        System.out.println("4. Exit");
    }

    static void newContact() {
        contactList.add(new Contact(input.confirmInput("first name"), input.confirmInput("last name"), input.confirmInput("address"), input.confirmInput("phone number"), input.confirmInput("DOB in dd/mm/yyyy format"), input.confirmInput("email")));
        showMenu();
    }

    static void updateExistingContact() {
        checkForContacts();
        displayContacts();
        try {
            updateContactFields(contactList.get(Integer.parseInt(input.confirmInput("contact choice")) - 1));
        } catch (Exception e) {
            System.out.println("No such contact");
            showMenu();
        }
    }

    static void checkForContacts() {
        if (contactList.size() == 0) {
            System.out.println("There are no contacts yet");
            showMenu();
        }
    }

    static void updateContactFields(Contact contact) {
        System.out.println("Currently: " + contact.returnFirstName());
        contact.updateFirstName(input.confirmInput("first name"));
        System.out.println("Currently: " + contact.returnLastName());
        contact.updateLastName(input.confirmInput("last name"));
        System.out.println("Currently: " + contact.returnAddress());
        contact.updateAddress(input.confirmInput("address"));
        System.out.println("Currently: " + contact.returnPhoneNumber());
        contact.updatePhoneNumber(input.confirmInput("phone number"));
        System.out.println("Currently: " + contact.returnDOB());
        contact.updateDOB(input.confirmInput("DOB in dd/mm/yyyy format"));
        System.out.println("Currently: " + contact.returnEmail());
        contact.updateEmail(input.confirmInput("email"));
        showMenu();
    }

    static void showContact() {
        checkForContacts();
        displayContacts();
        Contact contact = null;
        try {
            contact = selectContact(contactList.get(input.contactChoice() - 1));
        } catch (Exception e) {
            System.out.println("No such contact");
            showMenu();
        }
        printContactDetails(contact);
        showMenu();
    }

    static void printContactDetails(Contact contact) {
        System.out.println("First name is: " + contact.returnFirstName());
        System.out.println("Last name is: " + contact.returnLastName());
        System.out.println("Address is: " + contact.returnAddress());
        System.out.println("Phone number is: " + contact.returnPhoneNumber());
        System.out.println("Date of birth is: " + contact.returnDOB());
        System.out.println("Email is: " + contact.returnEmail());
    }

    static Contact selectContact(Contact contact) {
        return contact;
    }

    static void displayContacts() {
        System.out.println("Please select a contact");
        for (int i = 0; i < contactList.size(); i++) {
            System.out.println(i + 1);
        }
    }
}

输入菜单选择(>)>

    public Integer menuChoice() {
        Scanner userInput = new Scanner(System.in);
        String input = userInput.nextLine();
        try {
            return Integer.parseInt(input);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

编辑:在每种情况下添加了break语句。

上下文我有一个Java CLA,它内置于IntelliJ中,并由Gradle管理。用户在给定菜单中输入数字以添加/更改/查看联系人,然后退出。问题开关有问题...

java switch-statement
2个回答
1
投票

对于它们'掉进去'的开关格表达式,这是正常的。因此,如果我们也不想执行其他情况,则必须在每种情况下都中断。


0
投票
 If you want to execute only one case then you have to add break statement 
© www.soinside.com 2019 - 2024. All rights reserved.