如何结束我的while循环

How to end my while loop?

本文关键字:我的 while 循环 结束 何结束      更新时间:2023-10-16

嘿,谁能帮我一把?如何在第一次计算后使循环结束?完成加法等一次计算后,我想结束它。对不起菜鸟问题。我现在只是在课堂上学习循环。非常感谢。

#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;
}