【发布时间】:2020-05-17 23:19:13
【问题描述】:
标题可能有点不直观,但我不知道该怎么解释。
这就是我正在为一项任务设计一个“应用程序”的交易,其中一个要求是应用程序的 UI 是基于文本的,并且使用 MVC 模式工作。
我一直在努力实现的一个方面是有效地“后退/返回”菜单。
这是我想出的最好的东西:
public class Menu{
Scanner read;
public int MainMenu(){
System.out.println("############# !!! Main Menu !!! #############\n");
System.out.println("1) Shop"); //there's more options but I want to keep short and I only need one to demonstrate my troubles
System.out.println("0) Quit"); //terminates the application
int e = -1;
while (e<0 || e>4){
e = read.nextInt();
if (e<0 || e>4)
System.out.println("Wrong choice");
}
return e; //this value goes to the controller that then redirects to fucntions such as the one we see bellow
}
public int ShopMenu(){
System.out.println("############# !!! Shop Menu !!! #############\n");
System.out.println("\nLogin with your acount. If you want to go back to the main menu type: -back.\n");
System.out.println("Type your email: ");
String email=read.next(); //this string is meant to be read in the Class that holds all the registred emails so it can verify it's validity, but that's a not a concern currently.
switch (email){
case "-back": MainMenu(); email=""; email = read.next() ; break;
}
return 1;
}
}
虽然我可以使用此解决方案返回主菜单,但 我认为 email 字符串已“用完”(因为 init 不再为空,用户可以'不要再写了),下次我选择商店菜单时,我无法输入另一封电子邮件,程序停止运行。
我的意图是一旦 ShopMenu 功能结束,电子邮件字符串被清除,用户可以继续使用它来插入其他电子邮件,而应用程序终止。所有这些都不会明显影响返回上一个菜单的能力。
--编辑--
应 MarsAtomic 的要求,这是程序当前状态的输出:
####### !!!主菜单 !!!1) 商店
0) 退出
1
####### !!!商店菜单!!!使用您的帐户登录。如果要返回主菜单类型: -返回。
Insira o 电子邮件:
-返回
####### !!!主菜单 !!!1) 商店
0) 退出
1
-返回
进程以退出代码 0 结束
【问题讨论】:
-
“用完”是什么意思?描述你看到的,而不是你认为正在发生的,因为我很确定你没有看到你认为你看到的。更好的是,为了消除所有疑问,运行您的程序并复制并粘贴输出并将其编辑到您的问题中。
-
我想说电子邮件字符串不再是空的,我不能再在上面写/输入任何东西了
-
好的,我们又来了……为什么您认为电子邮件“不再为空”?你的意思是当你的程序第二次提示输入电子邮件地址时,它只是打印提示然后退出而不接受输入?这就是输出似乎所描绘的,如果这是真的,那么正在发生的事情并不是你认为正在发生的事情。
-
我正在学习java,所以我说我认为这就是发生的事情,因为这是我根据对输出的观察以及我使用java的少量经验和知识得出的结论。反正。对于我应该如何处理这个问题,您或任何人可以给我任何建议或可能的解决方案吗?
-
@Mr.Maleiro 这是一个反问。他试图帮助你检查你的假设。发现您的假设是解决您自己制造的问题的第一步。检查它们是第二步。
标签: java user-interface model-view-controller input