Try-Catch aider. Comment puis-je la boucle

0

La question

Pour la deuxième partie, où il est dit nouvel ordre, si je mets '5", il serait de dire "s'il vous plaît choisir une option valide" et la boucle de retour vers le "Bienvenue à Pied----" partie.

Comment puis-je obtenir seulement en boucle à l'endroit où ils sont entrés dans l'invite à tort?

Par exemple: si j'entre 5, il serait de dire "s'il vous Plaît choisir l'option valide", puis d'afficher le "nouvel ordre" et pas le "Bienvenue---"

public static void main(String[] args) {
        Assignment2A_JasmineLimSmithh myref = new Assignment2A_JasmineLimSmithh();
        while (true) {
        myref.run();
    }
    }
    void run() {
        System.out.println("Welcome to Foot Loose Reflexology");
        System.out.println("Main Menu \n 1. add new order \n 2. update order \n 3. view all orders \n 4. exit");
        System.out.print("Enter your choice [1...4] ");
        int a = 0;

        try {
            a = scan.nextInt();
        } catch (Exception e) {
            System.out.println("Please choose a valid option!");
            return;
        }

        if (a > 4 || a < 1) {
            System.out.println("Please choose a valid option!");
            return;
        }
        if (a == 1)
            newOrder();
        else if (a == 2)
            updateOrder();
        else if (a == 3)
            displayOrders();
        else if (a == 4)
            System.exit(0);
    }
void newOrder() {
        int name, choices = 0, duration = 0, order;
        System.out.println();
            do { // check for space?
                System.out.println("Add new order \n Reflexologist name: \n 1. Mark \n 2. James \n 3. Lily");
                System.out.print("Enter your choice [1...3] ");

                try {
                    name = scan.nextInt();
                } catch (Exception e) {
                    System.out.println("Please choose a valid option!");
                    return;
                }

                if (name > 3 || name < 1) {
                    System.out.println("Please choose a valid option!");
                    return;
                }

            } while (name > 3 || name < 1);
java
2021-11-24 04:04:39
1

La meilleure réponse

1

Se débarrasser de la return à l'intérieur de l'instruction if

        do { // check for space?
            System.out.println("Add new order \n Reflexologist name: \n 1. Mark \n 2. James \n 3. Lily");
            System.out.print("Enter your choice [1...3] ");

            try {
                name = scan.nextInt();
            } catch (Exception e) {
                System.out.println("Exception - wrong input type");
                return;
            }

            if (name > 3 || name < 1) {
                System.out.println("Please choose a valid option!");
                // return; <-- removed
            } 

        } while (name > 3 || name < 1);
2021-11-24 04:14:01

Dans d'autres langues

Cette page est dans d'autres langues

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................