Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_Scope_Submenu - Fatal编程技术网

如何在Java中创建主菜单?

如何在Java中创建主菜单?,java,arrays,scope,submenu,Java,Arrays,Scope,Submenu,好的,我有一个澄清,因为这个问题有点误导--我知道如何创建菜单、列出选项和提示输入、读取用户输入等 我遇到的问题纯粹是合乎逻辑的。我需要创建一个包含3个选项的主菜单: -创建具有关联属性的学生“对象”数组 -搜索列表 -出口 在这一点上,我有布尔操作,而激活“子菜单”的循环。布尔值根据是否要创建列表、搜索等而变为true或false (重要的是要知道,我有一个高于(学生)的类,它包含数组的所有属性,如名称、平均成绩等)。我的主程序的总体布局如下: while(bMainMenu)

好的,我有一个澄清,因为这个问题有点误导--我知道如何创建菜单、列出选项和提示输入、读取用户输入等

我遇到的问题纯粹是合乎逻辑的。我需要创建一个包含3个选项的主菜单:

-创建具有关联属性的学生“对象”数组

-搜索列表

-出口

在这一点上,我有布尔操作,而激活“子菜单”的循环。布尔值根据是否要创建列表、搜索等而变为true或false

(重要的是要知道,我有一个高于(学生)的类,它包含数组的所有属性,如名称、平均成绩等)。我的主程序的总体布局如下:

    while(bMainMenu)
        {
            boolean bList = false;
            boolean bSearch = false;

            [all the code prompting choices]
                -if input is "create" then bList = true;
                -if input is "search" then bSearch = true;
                -if input is "exit" then bMainMenu = false;

            while(bList)
                { 
                    [all the code that creates the array and prompts for student info
                     where the array is as long as the user chooses]
                    bList = false;
                }

            while (bSearch)
                {
                    [all the code for searching the array]
                    bSearch = false;
                }

        }
创建列表后,其他所有内容都为false,并重新循环主菜单。这一次,用户说“搜索”,它使搜索菜单的布尔值为真

我遇到的问题是:引用刚刚创建的数组。我在想我应该在哪里打电话。据我所知,我刚刚完成所有工作的方式使得数组只包含在“bList循环”中

我在哪里调用数组以使其对“bSearch”循环“可见”?还是我需要以不同的方式重组一切


提前谢谢

这都是关于范围的。类似于您可以访问布尔变量
bList
bSearch
在主外循环中的任何位置:
while(bMainMenu)
,您可以在那里类似地声明学生数组:

while(bMainMenu) {
   Student[] students;
   boolean bList = false;
   boolean bSearch = false;

   // Remaining code can now access students array as it is in scope
}
然后稍后实例化数组,例如:
students=newstudent[numberStudents]

如果你不知道有多少学生将被预先创建,或者如果它将继续增长,请考虑<代码> ARAYLIST/<代码>。< /P> <代码> BMIN菜单= TRUE;

bMainMenu = true;
List<StudentInfo> students = new ArrayList<StudentInfo>();
while(bMainMenu){

    boolean bList = false;
    boolean bSearch = false;

    [all the code prompting choices]
                -if input is "create" then bList = true;
                -if input is "search" then bSearch = true;
                -if input is "exit" then bMainMenu = false;

    while(bList){
        [all the code that creates the array and prompts for student info
                     where the array is as long as the user chooses]

        // Build this student info object based on user parameters
        StudentInfo studentInfo = new StudentInfo(name, class);

        students.add(studentInfo);
    }

    while(bSearch){
        /*      
        Play with "students" arraylist. It is accessible here and contains all studentinfo details added before.
        */
    }
}
List students=new ArrayList(); while(b主菜单){ 布尔bList=false; 布尔b搜索=假; [所有代码提示选项] -如果输入为“创建”,则bList=true; -如果输入为“搜索”,则bSearch=true; -如果输入为“退出”,则bMainMenu=false; while(bList){ [创建数组和提示输入学生信息的所有代码 其中阵列的长度由用户选择] //基于用户参数生成此学生信息对象 StudentInfo StudentInfo=新的StudentInfo(名称、类别); 添加(studentInfo); } while(b搜索){ /* 玩“学生”数组列表。它可以在这里访问,并包含以前添加的所有学生信息详细信息。 */ } }
阵列需要位于作用域中,以便必要的块可以访问它。如果它在一个while块中,那么它将只对该块可见。太棒了。谢谢我当时不知道在没有实例化的情况下声明数组——这会改变一切!我考虑过做ArrayList的事情,但是我的教授现在只想让我这样做,然后我们会在本学期晚些时候做ArrayList。事实上,我用你发布的方式做的问题是,它需要我初始化变量——它直到第一个while循环才初始化,我仍然对范围有同样的问题。Eclipse的建议是说“Student[]students=Null”;但显然,这是错误的。。。