如何结束我的while循环
How to end my while loop?
嘿,谁能帮我一把?如何在第一次计算后使循环结束?完成加法等一次计算后,我想结束它。对不起菜鸟问题。我现在只是在课堂上学习循环。非常感谢。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int numOne;
int numTwo;
int result;
string operation;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation == "e" || operation == "E")
{
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
while (operation == "+" || operation == "-" || operation == "*" || operation == "/")
{
cout << "Please enter integer one: ";
cin >> numOne;
cout << "Please enter integer two: ";
cin >> numTwo;
if (operation == "+")
{
result = numOne + numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "-")
{
result = numOne - numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "*")
{
result = numOne * numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "/")
{
if (numTwo == 0)
{
cout << "You cannot divide by zero!" << endl;
}
else
{
result = numOne / numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
}
}
return 0;
}
我不允许使用break
.还有别的办法吗?
您可以使用以下命令结束 while 循环...
break;
即
if(!(operation == "e" || operation == "E")) {
break;
}
通常结束while
循环(EXPRESSION)
在以下即
while((EXPRESSION) == true) { execute_code_here();}
需要评估为假。表达式可以由许多逻辑部分组成,即
while (a == b && c != d && f++ < 1000) {do_something_here();}
break
关键字用于提前终止 while 循环,但这也可以通过在表达式中添加一些允许您终止 while 循环的东西来实现,即使表达式的计算结果为 false,这通常使用标志或计数器来完成。
要在不使用的情况下获得与break
相同的效果,您可以将此技术与 continue
.然后,您将标志或计数器与continue
一起使用,以实现与break相同的操作,即这是一个计数器示例
while(a == b && flag++ < 1000) {
if(this_returns_true()) {
flag = 10000;
continue;
}
/*The code here may not be executed*/
}
使用布尔标志将是...
while(a == b && flag == true) {
if(this_returns_true()) {
flag = false;
continue;
}
/*The code here may not be executed*/
}
只需将while
更改为if
即可。如果您只想执行一次,则不需要循环。
您可以在 while 循环中将break;
与 if
语句一起使用。
这样,如果true
条件,您将从while循环中脱颖而出。
例
if (operation != "e")
break;
如果你不被允许使用break
,我建议你不要使用循环。只需计算一次即可。
我对C++不是很有经验,但我认为你的意思是break;
陈述
https://msdn.microsoft.com/library/37zc9d2w.aspx
使用
break;
这将每次结束循环。您可以在它周围包装一个 if 语句以应用退出条件。
如果你必须使用 while 循环,只需添加 break; 这将打破循环。如果您不必这样做,我会将其更改为 if 语句。示例:if(varName == '+'({
Num1 + num2;}
抱歉格式,在移动设备上
代码块:
cout << "Please enter what operation you'd like to perform or e/E to end program: "; cin >> operation; while (operation == "e" || operation == "E") { cout << "Operation type invalid." << endl; cout << "Please enter what operation you'd like to perform or e/E to end program: "; cin >> operation; }
不执行您向用户承诺的操作:
Please enter what operation you'd like to perform or e/E to end program:
如果输入"e"
或"E"
,循环将不会停止并再次请求输入。
你可能想要这样的东西:
do {
cin >> operation;
while (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// ...
}
} while(operation != "e" && operation != "E");
至于你愚蠢的限制不使用do {} while();
(踢你的教授的屁股,至少做两次(:
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while(operation != "e" && operation != "E") {
if (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// ...
}
else {
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
}
我想你实际上想要实现的是这个。第一个循环应该被重构(即:循环,直到操作是 neher 'e' 或 'E'(,第二个循环应该是 if-else-语句(检查操作是否有效(。
int main()
{
int numOne;
int numTwo;
int result;
string operation;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation != "e" && operation != "E") {
if (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// your valid operation code...
} else {
cout << "Operation type invalid." << endl;
}
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
return 0;
}
- 为什么我的 while 函数没有终止?
- 为什么我的 while 循环对于特定输入中断,而对于其他输入工作正常?
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- 我的 if 语句打印在我的 while 循环中的每个 if 语句上,无论它是否为假
- 如何调试我的 while 循环C++程序?
- 为什么我的 while 语句没有结束?
- 为什么我的 While 循环中不断出现"expected expression error"?
- 为什么我的 while 循环函数只返回"1"?
- 第一次执行后我的while循环周围出现错误,不再要求输入
- 我的while循环条件没有像我预期的那样评估它们
- 我的 while 循环不会在条件下停止
- 我没有正确编程我的while循环
- 在我的switch语句完全执行之前,我的while循环进入第二次迭代,我用c ++编码
- 如何结束我的while循环
- 为什么我的 while 循环会导致无限输出
- 我无法在我想要的时候让我的 while 循环停止
- 我的while循环似乎忽略了我的getline
- 我的while循环过早退出
- 为什么我的"while"条件没有得到满足?
- 我的while程序溢出了?我做错了什么?