I am trying to stop a loop for my calculator program, but I am not allowed to use breaks or system.exit. How would I go so with stopping my loop my just putting in 5 for the menu? Also How do I make the program quit if I would put in "n" for the question asking would I like to use the calculator again? P.S. I am using Java.
我试图阻止我的计算器程序循环,但我不允许使用break或system.exit。如何停止我的循环我只需要输入5个菜单?另外,如果我在问题中输入“n”,那么如何让程序退出?我想再次使用计算器吗?附:我正在使用Java。
public static void main (String args []) {
String MainMenu = " Calculator Options \n\n1. Addition\n2" +
" Subtraction\n3. Multiplication\n4. Division\n5. Exit";
menu (MainMenu);
while (true) {
String YesorNo = JOptionPane.showInputDialog("Would You Like to Use the Calculator Again (y / n)? ");
if (YesorNo.equals ("y")) {
menu (MainMenu);
}
if (YesorNo.equals ("n")) {
}
}
}
public static int menu (String info) {
String choice = JOptionPane.showInputDialog (info);
if (choice.equals ("1"))
return Addition();
else if (choice.equals ("2"))
return Subtraction();
else if (choice.equals ("3"))
return Multiplication();
else if (choice.equals ("4"))
return Division();
else
return 0;
}
2 个解决方案
#1
2
Use the break
keyword.
使用break关键字。
if (YesorNo.equals ("n")) {
break;
}
Or place the condition in the while-loop
and use the keyword do
.
或者将条件放在while循环中并使用关键字do。
do {
// code & blaaa .. blaaa & code
} while (YesorNo.equals("y"));
#2
2
To answer both of you questions:
回答你们两个问题:
You simply need to use a do-while
loop and include both conditions in there. But With your current implementation that wont be working 100% of the time.
您只需要使用do-while循环并在其中包含两个条件。但是,您目前的实施不会100%的时间都在工作。
Reason beeing, you return the action aswell as the result from menu
.
理由是,您返回动作以及菜单的结果。
To start of, that´s how your loop could look like.
首先,这是你的循环的样子。
do {
String YesorNo = JOptionPane.showInputDialog ("Would You Like to Use the Calculator Again (y / n)? ");
int menuReturn = menu (MainMenu);
} while (YesorNo.equals("y") || menuReturn != 0);
That´s what could work, the menu
method does return 0
if it is not inside the values 0-4
. But what happens when the result of any of your mathemematical operations is 0
? You would also close the calculator. At this point you might want to change what menu
does.
这就是可行的,如果菜单方法不在值0-4内,则返回0。但是当你的任何数学运算的结果为0时会发生什么?你也可以关闭计算器。此时您可能想要更改菜单的功能。
private static int result = 0;
public static int menu (String info) {
String choice = JOptionPane.showInputDialog (info);
if (choice.equals ("1")) {
result = Addition();
return 1;
}
else if (choice.equals ("2")) {
result = Subtraction();
return 1;
}
else if (choice.equals ("3")) {
result = Multiplication();
return 1;
}
else if (choice.equals ("4")) {
result = Division();
return 1;
}
else if (choice.equals("5"))
return -1;
else
return 0;
your loop design would change now to something like the following.
你的循环设计现在会改变为类似下面的东西。
do {
String YesorNo = JOptionPane.showInputDialog ("Would You Like to Use the Calculator Again (y / n)? ");
int menuReturn = menu (MainMenu);
// Work with the result now
if (menuReturn == 0) // invalid input;
} while (YesorNo.equals("y") || menuReturn != -1);
But in order to make it fully contract the OOP principle you might have to rewrite a little bit more code then shown in the answer.
但是为了使其完全符合OOP原则,您可能需要重写一些代码,然后在答案中显示。
#1
2
Use the break
keyword.
使用break关键字。
if (YesorNo.equals ("n")) {
break;
}
Or place the condition in the while-loop
and use the keyword do
.
或者将条件放在while循环中并使用关键字do。
do {
// code & blaaa .. blaaa & code
} while (YesorNo.equals("y"));
#2
2
To answer both of you questions:
回答你们两个问题:
You simply need to use a do-while
loop and include both conditions in there. But With your current implementation that wont be working 100% of the time.
您只需要使用do-while循环并在其中包含两个条件。但是,您目前的实施不会100%的时间都在工作。
Reason beeing, you return the action aswell as the result from menu
.
理由是,您返回动作以及菜单的结果。
To start of, that´s how your loop could look like.
首先,这是你的循环的样子。
do {
String YesorNo = JOptionPane.showInputDialog ("Would You Like to Use the Calculator Again (y / n)? ");
int menuReturn = menu (MainMenu);
} while (YesorNo.equals("y") || menuReturn != 0);
That´s what could work, the menu
method does return 0
if it is not inside the values 0-4
. But what happens when the result of any of your mathemematical operations is 0
? You would also close the calculator. At this point you might want to change what menu
does.
这就是可行的,如果菜单方法不在值0-4内,则返回0。但是当你的任何数学运算的结果为0时会发生什么?你也可以关闭计算器。此时您可能想要更改菜单的功能。
private static int result = 0;
public static int menu (String info) {
String choice = JOptionPane.showInputDialog (info);
if (choice.equals ("1")) {
result = Addition();
return 1;
}
else if (choice.equals ("2")) {
result = Subtraction();
return 1;
}
else if (choice.equals ("3")) {
result = Multiplication();
return 1;
}
else if (choice.equals ("4")) {
result = Division();
return 1;
}
else if (choice.equals("5"))
return -1;
else
return 0;
your loop design would change now to something like the following.
你的循环设计现在会改变为类似下面的东西。
do {
String YesorNo = JOptionPane.showInputDialog ("Would You Like to Use the Calculator Again (y / n)? ");
int menuReturn = menu (MainMenu);
// Work with the result now
if (menuReturn == 0) // invalid input;
} while (YesorNo.equals("y") || menuReturn != -1);
But in order to make it fully contract the OOP principle you might have to rewrite a little bit more code then shown in the answer.
但是为了使其完全符合OOP原则,您可能需要重写一些代码,然后在答案中显示。