好吧,我有一个澄清,因为这个问题有点误导!--我知道如何创建菜单、列出选择和提示输入、读取用户的输入等。
我遇到的问题纯粹是逻辑上的。我需要创建一个有 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/