Spróbuj-Złap pomoc. Jak zrobić pętlę

0

Pytanie

W drugiej części, gdzie jest mowa o nowym zamówieniu, jeżeli ustawię "5", będzie napisane "proszę wybrać odpowiedni wariant" i wróć do części "witamy w ft".

Jak mi się zrobić tak, aby wracał tylko tam, gdzie są one błędnie zaproszenie?

Na przykład: jeśli dam ci 5, na nim będzie napisane "Proszę wybrać odpowiedni wariant", a następnie pojawi się część "Nowe zamówienie", a nie "witamy..."

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

Najlepsza odpowiedź

1

Pozbądź się return w wewnętrznej instrukcji 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

W innych językach

Ta strona jest w innych językach

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