c – 如果条件为真,则返回if条件,如果为真,则返回

c – 如果条件为真,则返回if条件,如果为真,则返回,第1张

概述当条件为真或假时,如何让它返回并再次提出问题,让用户重新输入值? 这是我想要实现的: #include <iostream>#include <string>#include <sstream>using namespace std;int main(){ int n; cout<<"Enter numbers. Press 5 to stop: "; cin> 当条件为真或假时,如何让它返回并再次提出问题,让用户重新输入值?
这是我想要实现的:

#include <iostream>#include <string>#include <sstream>using namespace std;int main(){    int n;    cout<<"Enter numbers. Press 5 to stop: ";    cin>>n;    bool tr=true;    while(tr)    {        if(n!=5)            cout<<"You entered "<<n; //How to make it return again,since its false? I keep getting infinite loops :( ;        else             tr=false;    }    return 0;}
解决方法 您需要在while循环中提示用户,以便它在每次迭代中发生:

int n;bool tr = true;while(tr){  cout << "Enter numbers. Press 5 to stop: ";  cin >> n;  if(n!=5) {    cout << "You entered " << n;  } else {    tr = false;  }}
总结

以上是内存溢出为你收集整理的c – 如果条件为真,则返回if条件,如果为真,则返回全部内容,希望文章能够帮你解决c – 如果条件为真,则返回if条件,如果为真,则返回所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1214809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存