Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java返回到程序中的特定点_Java - Fatal编程技术网

java返回到程序中的特定点

java返回到程序中的特定点,java,Java,我有下一个问题。我有一个结构如下的程序: loadContext(); showCategories(input); showProjects(input); showdetails(); 在每一步上,我作为一个用户都可以跳到上一步。例如,我按下0,程序返回到previos步骤。按1-程序从头开始。Java中有什么工具可以达到特定的目的吗 编辑: 这里有一个控制台应用程序。主要方法如下所示: loadContext();//Loads categories showCategories(inp

我有下一个问题。我有一个结构如下的程序:

loadContext();
showCategories(input);
showProjects(input);
showdetails();
在每一步上,我作为一个用户都可以跳到上一步。例如,我按下0,程序返回到previos步骤。按1-程序从头开始。Java中有什么工具可以达到特定的目的吗

编辑: 这里有一个控制台应用程序。主要方法如下所示:

loadContext();//Loads categories
showCategories(input);//shows available categories and ask for which category to show
showProjects(input);// shows all projects inside selested category and select which project to show in details
showdetails();//show selected project

现在我想设置选项。例如,在showProjects(输入)中,放置0并再次查看类别,选择它并查看类别。在showdetails()中选择0并返回显示类别,选择一个,依此类推。

您可以执行以下操作:

   private void processUserInput(int selOption) {
       if(selOption == 0){
           showCategories();
       }
       else if(selOption == 1){
           programStart();
       }
       else{
          System.out.println("Unsupported option");                  
       }
   }  
  • 将所有四个方法包装到另一个方法中:

       private void programStart(){
           loadContext();
           showCategories(input);
           showProjects(input);
           showdetails();
       }
    
  • 然后有一个这样的方法:

       private void processUserInput(int selOption) {
           if(selOption == 0){
               showCategories();
           }
           else if(selOption == 1){
               programStart();
           }
           else{
              System.out.println("Unsupported option");                  
           }
       }  
    
  • 在每个方法的末尾,您希望此选项可用:

    a。从用户处读取选项
    B使用输入调用
    processUserInput(输入)


  • while循环中的switch语句怎么样?允许用户选择要跳转到的步骤。这是任何语言的工具,而不仅仅是Java,可以到达任何特定的点

    我使用java.util.Scanner是因为您希望它成为一个控制台应用程序,尽管2015年我们倾向于使用图形用户界面或网页来完成这项工作:

    import java.util.Scanner;
    
    public class BackNForth {
    
        private static int input;
    
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.print("select: ");
            int sel = Integer.parseInt(s.nextLine());
            while (sel >= 1 && sel <= 4) {
                switch (sel) {
                    case 1:
                        loadContext();
                        break;
                    case 2:
                        System.out.print("Cat input: ");
                        input = Integer.parseInt(s.nextLine());
                        showCategories(input);
                        break;
                    case 3:
                        System.out.print("Pro input: ");
                        input = Integer.parseInt(s.nextLine());                    
                        showProjects(input);
                        break;
                    case 4:
                        showdetails();
                        break;
                }
                System.out.print("select: ");
                sel = Integer.parseInt(s.nextLine());
            }
        }
    
        private static void loadContext() {
            System.out.println("loadContext");
        }
    
        private static void showCategories(int input) {
            System.out.println("showCategories " + input);
        }
    
        private static void showProjects(int input) {
            System.out.println("showProjects " + input);
        }
    
        private static void showdetails() {
            System.out.println("showdetails");
        }
    }
    
    import java.util.Scanner;
    公共类BackNForth{
    私有静态int输入;
    公共静态void main(字符串[]args){
    扫描仪s=新的扫描仪(System.in);
    系统输出打印(“选择:”);
    int sel=Integer.parseInt(s.nextLine());
    
    虽然(sel>=1&&sel如果你有接受输入的代码,比如你提到的
    0,1
    ,你应该输入代码来调用相应的函数。例如
    0
    是第一个函数
    1
    是第二个函数等。因此,如果我在第三个函数中,需要时点击
    0
    ,它将运行第一个函数。你可以我们需要更具体地帮助您