循环而不使用while循环 [英] Loop without the use of while loop

查看:91
本文介绍了循环而不使用while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想知道以下情况,是否可以在不使用while循环的情况下继续提示用户输入(输出窗口不会关闭)?



如果我不清楚/我的英语不好,假设如果用户输入20,它将返回输出'错误!'但它会重新提示用户重新输入一个新数字



可以这样做吗?



Hi all, I was wondering for the following, is it possible to keep prompting user input (output window will not be close) without the use of while loops?

In case if I am not clear/ my english is no good, suppose if the user inputs in 20, it will return the output 'Wrong!' but it will re-prompts the user to re-enter in a new number

Can that be done?

#include <iostream>
using namespace std;

bool numCheck (int num);

int main(){
    int input;
    cout << "Enter in a number : ";
    cin >> input;
    
    if (numCheck(input)){
        cout << "Correct!" << endl;
    }
    else{
        cout << "Wrong!" << endl;
    }
}

bool numCheck (int num){
    if (num == 10){
        return true;
    }
    else{
        return false;
    }
}





我的尝试:



我知道使用while循环可能是它的理想解决方案,但是想知道是否有任何其他替代方法,就像我发布的代码一样简单。



What I have tried:

I know that using of while loop could possible be the ideal solution for it, but would like to know if there are any other alternatives for something as simple as the code I have posted..

推荐答案

根据您的要求,您应该使用 / a> [ ^ ]而不是像你一样允许用户在检查条件之前至少输入一次。
Based on your requirement, you should use do while[^] instead of while as you have to allow a user to enter at least once before checking the condition.


参见迭代语句(C ++) [ ^ ]。


Quote:

循环没有使用while循环

Loop without the use of while loop

这是可能的,但是没有必要这样做。

在C中有很多时尚做循环但是所有的几乎都是相同的结果,同样的容量,效率相同。

所以没有理由避免一种循环。

It is possible, but there is no point doing so.
There is many fashions to do loops in C but all fall in pretty much the same result, same capacities, same efficiency.
So there is no reason to avoid 1 kind of loop.


这篇关于循环而不使用while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆