我正在为我的某个课程生成一个菜单驱动的计算程序,并且我试图弄清楚为什么我的子菜单在完成之后不会返回到根菜单。使用主参数集,选项1-4和6可以正常工作。选项5将拉出子菜单并排队正确的响应,但然后只是转储回子菜单循环。我缺少什么,或者有没有办法打破并返回root而不终止程序?谢谢你的帮助!
主要论点:
t
主菜单:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
x = np.linspace(-1, 1, 101)
t = 0
y = (1/(1+16*(x-t)**2))
line, = ax.plot(x, y, lw=2)
ax.set_xlabel('x'), ax.set_ylabel('f(x,t)')
tfin = 1
k = 0.1
ts = np.arange(0, tfin, k)
def animate(i):
t = ts[i]
y = (1/(1+16*(x-t)**2))
line.set_data(x, y)
ani = animation.FuncAnimation(
fig, animate, interval=100, frames=len(ts), repeat=True)
plt.show()
子菜单:
public static void main(String[] args)
{
Final_Project project = new Final_Project();
int selection = 0;
while (selection != 6)
{
selection = project.mainMenu();
if (selection == 1)
project.addition();
else if (selection == 2)
project.subtraction();
else if (selection == 3)
project.multiplication();
else if (selection == 4)
project.division();
else if (selection == 5)
{
project.subMenu();
int subSelection = 0;
while (subSelection != 2)
{
subSelection = project.subMenu();
if (subSelection == 1)
project.displayAdditionTable();
else if (subSelection == 2)
project.displayMultiplicationTable();
else
System.out.println("Invalid menu options selected, try again\n");
}
}
else if (selection == 6)
project.exitProgram();
else
System.out.println("Invalid menu options selected, try again.\n");
}
}
表格占位符示例:
private int mainMenu()
{
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to the Java Calculator!\n");
System.out.println("MAIN MENU\n");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Display Study Tables");
System.out.println("6. Exit\n");
System.out.println("Please select an option from the menu:");
return kb.nextInt();
}
答案 0 :(得分:0)
您的选择== 5阻止有几个问题。
我不想为你编写代码,因为这是一个类,但我会看看你是否可以将所有子菜单代码(包括循环)移动到subMenu()方法中,并且只调用项目。来自主循环的subMenu()。否则,主循环必须知道关于子菜单选项是什么以及它应该返回什么的太多信息,这是不好的封装。