java - 如何用Java创建主菜单?

标签 java arrays scope submenu

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

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

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

-搜索列表

-退出

.

此时,我已经有了 boolean 操作的 while 循环来激活“子菜单”。 boolean 值根据是否要创建列表、搜索等而变为 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;
                }

        }

创建列表后,其他一切都是错误的,并且它重新循环主菜单。这次,用户说“搜索”,搜索菜单的 boolean 值为 true。

我遇到的问题:引用刚刚创建的数组。我想弄清楚我需要在哪里调用它。据我所知,我刚刚完成所有操作的方式使得数组仅包含在“bList 循环”中。

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

提前致谢!

最佳答案

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.
        */
    }
}

关于java - 如何用Java创建主菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22007078/

相关文章:

java - 如何以编程方式检查 Android 中互联网连接的可用性?

java - Android ROOM-如何在 RecyclerView 中显示来自 3 个相关实体的数据?

javascript - 如何到达内联 javascript 回调函数的父作用域?

java - While 循环字符串检查

java - 读取可能更改的图像文件路径

javascript - Angularfire $firebaseArray 扩展动态计数\总和

javascript - 在javascript中相交多维数组

在 C 中执行 Rot13 时崩溃

javascript - 函数声明 - 函数表达式 - 作用域

ruby-on-rails - Rails 中的链接作用域方法