简体   繁体   中英

Calling methods from other methods in the same class

I am trying to call a method from another method in the same class, for example when the "enterValues" method is finished, I want it to go back to the main menu. Can someone please explain how I can do this? I am also a bit confused on the use of objects here, am I right in thinking I need to create an object in every method like I have done here, in order to call other methods?

    import java.util.Scanner;
public class Conversion {

    int value;

    public void mainMenu() {
        int menuChoice;

        Scanner menuScan = new Scanner(System.in);

        Conversion mainMenu = new Conversion();

        System.out.println("1. Enter values and type -1 to stop");
        System.out.println("2. Euros");
        System.out.println("3. Dollars");
        System.out.println("4. Yen");
        System.out.println("5. Rupees");
        System.out.println("6. Exit");


        while (!menuScan.hasNextInt() || (menuChoice = menuScan.nextInt()) > 6) {
            menuScan.nextLine();
            System.err.println("Please enter a valid menu option 1 - 6: "); 
        }


        switch (menuChoice) {
        case 1:

            mainMenu.enterValues();

        case 2:

        }


    }

    public void enterValues() {

        Conversion enterValues = new Conversion();

        Scanner valueScan = new Scanner(System.in);
        System.out.print("Enter value to convert: ");
        value = valueScan.nextInt();
        System.out.println("Value entered. Returning to main menu.");

        valueScan.close();

        enterValues.mainMenu();
    }

    public static void main(String[] args) {


        Conversion main = new Conversion();

        main.mainMenu();

    }

}

When you are inside a non-static method, you already are in an instance of your Class, so no need to create another instance.

Also, when you are in an instance of a class, you just call other methods directly, like mainMenu();

I modified your code a bit to reflect this :

import java.util.Scanner;
public class Conversion {

    int value;

    public void mainMenu() {
        int menuChoice;

        Scanner menuScan = new Scanner(System.in);

        System.out.println("1. Enter values and type -1 to stop");
        System.out.println("2. Euros");
        System.out.println("3. Dollars");
        System.out.println("4. Yen");
        System.out.println("5. Rupees");
        System.out.println("6. Exit");


        while (!menuScan.hasNextInt() || (menuChoice = menuScan.nextInt()) > 6) {
            menuScan.nextLine();
            System.err.println("Please enter a valid menu option 1 - 6: "); 
        }


        switch (menuChoice) {
        case 1:

            enterValues();

        case 2:

        }


    }

    public void enterValues() {


        Scanner valueScan = new Scanner(System.in);
        System.out.print("Enter value to convert: ");
        value = valueScan.nextInt();
        System.out.println("Value entered. Returning to main menu.");

        valueScan.close();

        mainMenu();
    }

    public static void main(String[] args) {


        Conversion main = new Conversion();

        main.mainMenu();

    }

}

You must not create a new object every time you call a method. Within a class you can call any method you want.

If you finish one method, you continue where you called it. So in order to keep the main menu open you would have to use a loop or something similar.

The call itself is nothing more than:

mainMenu();

respectively

enterValues();

without creating a new Conversion .

You don't want to call mainMenu from enterValues , you want to return to it.

Make a "forever" loop inside mainMenu , and add an exit condition with a break . This way simply returning from enterValues or any other method inside mainMenu would bring you back to printing main menu and asking what else you wish to do:

public void mainMenu() {
    mainLoop: while (true) {
        int menuChoice;

        Scanner menuScan = new Scanner(System.in);

        Conversion mainMenu = new Conversion();

        System.out.println("1. Enter values and type -1 to stop");
        System.out.println("2. Euros");
        System.out.println("3. Dollars");
        System.out.println("4. Yen");
        System.out.println("5. Rupees");
        System.out.println("6. Exit");

        while (!menuScan.hasNextInt() || (menuChoice = menuScan.nextInt()) > 6) {
            menuScan.nextLine();
            System.err.println("Please enter a valid menu option 1 - 6: "); 
        }

        switch (menuChoice) {
        case 1:
            mainMenu.enterValues();
            break;
        case 2:
            break;
        case 6:
            break mainLoop;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM