c – 如果条件为真,则返回if条件,如果为真,则返回
发布时间:2020-12-16 09:42:53 所属栏目:百科 来源:网络整理
导读:当条件为真或假时,如何让它返回并再次提出问题,让用户重新输入值? 这是我想要实现的: #include iostream#include string#include sstreamusing namespace std;int main(){ int n; cout"Enter numbers. Press 5 to stop: "; cinn; bool tr=true; while(tr)
当条件为真或假时,如何让它返回并再次提出问题,让用户重新输入值?
这是我想要实现的: #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; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |