Try-Catch aiuto. Come faccio a loop

0

Domanda

Per la seconda parte, dove si dice di ordine nuovo, se ho messo '5' sarebbe dire "si prega di scegliere un'opzione valida" e il ciclo "Benvenuto a Piedi----" parte.

Come faccio a farlo solo loop di nuovo a dove hanno inserito il prompt torto?

Per esempio: se inserisco 5, sarebbe dire "si Prega di scegliere opzione valida" e quindi visualizzare il "nuovo ordine" parte " e non il "Benvenuto---"

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

Migliore risposta

1

Sbarazzarsi di return interno, se la dichiarazione

        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

In altre lingue

Questa pagina è in altre lingue

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