2021年4月7日星期三

How to get back to main menu after executing a function in switch

This is my code. I just want to ask how can I go back to the inputting what switch statement that I want to perform or my main menu after executing a function in the switch statement. It seems that goto function is not working here in java.

public class FINAL   {         public static void main(String[] args)       {          Scanner option = new Scanner (System.in);          int ch;          {          System.out.println("~~~~~~~~~~~~~~~~~~~~ WELCOME ~~~~~~~~~~~~~~~~~~~~");          System.out.println("");          System.out.println("[1] - New");          System.out.println("[2] - Open");          System.out.println("[3] - Save");          System.out.println("[4] - Save As");          System.out.println("[5] - Item Record");          System.out.println("[6] - Exit");          System.out.println("Choose what option do you want to perform.......");          ch = option.nextInt();                    switch(ch)          {              case 1:                  New();                  break;                        case 2:                  System.out.println("");                  System.out.println("");                  System.out.println("GFdfdf");                  break;          }          }        }  

I am creating a file in this function and after executing it in case 1, I want to go back to my main menu to choose what case in the switch that to perform

    static void New()      {          Scanner name = new Scanner  (System.in);                  String filename;                                    System.out.println("");                  System.out.println("");                  System.out.println("Enter file name: ");                  filename = name.nextLine();                  System.out.println("");                  System.out.println("");                                    try                   {                      File myfile = new File(filename);                      if (myfile.createNewFile())                      {                          System.out.println("File Created " + myfile.getName());                      }                      else                      {                          System.out.println("File already exist.");                      }                  }                  catch (IOException e)                  {                      System.out.println("An Error Occured");                      e.printStackTrace();                  }        }    }  
https://stackoverflow.com/questions/66995913/how-to-get-back-to-main-menu-after-executing-a-function-in-switch April 08, 2021 at 08:45AM

没有评论:

发表评论