如何循环一个if语句 [英] how to loop an if statment

查看:298
本文介绍了如何循环一个if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我想循环一个if语句,以便只有两个选择.我有以下内容:

Ok I want to loop an if statement so that there are only two choices as it were. I have as follows:

cout << "Please enter A to use a or P to use p:"; cin >>; apChoice; cout <<; endl;
    
if ((apChoice == ''A'') || (apChoice == ''a''))
{
  void aChoice();    
}
else if ((pChoice) == ''P'' || (pChoice == ''p''))
{
  void pChoice();
}
else
{
  cout <<; "Wrong choice please choose again.";
}



我想要这个最下面的else语句,以便如果有人输入除A或P之外的其他内容,它将再次询问该问题.我似乎无法正常工作.



this bottom else statement I want it so that if someone enters something other than A or P it asks the question again. I just can''t seem to get it to work.

推荐答案



最简单的无限循环:
Hi,

The simplest infinite loop:
for (;;)
{
   // your code 
}

这就是您所要求的,但现在您的用户被卡住了 :(
也许您应该添加退出命令 :)
欢呼声,
AR

阅读Ash的答案后删除了发布"

That''s what you requested but now your user is stuck in that block :(
Maybe you should add an exit command :)
cheers,
AR

removed ''as posted'' after reading Ash''s answer


除非您真的误解了您的意思,否则您的代码有些错误最多(可能...).

第一个是当你写的时候:

There''s a few things wrong with your code, unless I''ve really misunderstood what you''re up to (which is possible...).

The first one is that when you write:

void aChoice();





or

void pChoice();



您是在对函数进行原型设计,而不是对其进行调用.如果您真的想打电话给他们,则需要将void敲开.

我还要假设您在第二个条件语句中也表示aChoice.尽管您获得的代码可能会编译(不确定将函数指针转换为任意指针的规则),但是您正在做的事情是将函数pChoice的地址与几个字符进行比较.将编译器警告提高到最高级别(在Visual C ++上为/W4/WX)可能是值得的,因为这可能会为您提供更多指导.

现在,将其整理出来...看起来您想做的就是在用户未输入您期望的内容时循环,否则您想执行其中一个功能并退出该功能程序.如果您不想纾困,那么您就不想做我要建议的事情!

那么,如何循环直到输入达到您的期望?我很想使用一个while循环(一个do/while可能实际上更好地工作,我只是出于某种逻辑上的原因对我有点不利,因为我无法投入我的手指). while循环的条件类似于您稍后检查的两个条件:



you''re prototyping the functions, not calling them. If you really want to call them you need to knock the void off the front.

I''d assume you mean aChoice in the second conditional statement as well. While the code you''ve got may compile (not sure of the rules for converting function pointers to arbitrary pointers) what it looks like you''re doing is comparing the address of the function pChoice with a couple of characters. It might be worth cranking up your compiler warnings to the top level (/W4 /WX on Visual C++) as that might give you some more guidance.

Now, having sorted that out... What it looks like you want to do is loop around while the user hasn''t entered what you expected him or her to, otherwise you want to execute one of the functions and bail out of the program. If you don''t want to bail out then you don''t want to do what I''m going to suggest!!

So how do you loop until the input is what you expect? I''d be tempted to use a while loop (a do/while might actually work better, I''m just a bit adverse to them for no logical reason I can put my finger on). The conditional for the while loop is similar to the two conditions you check later:

char aChoice = '' '';

while( ( aChoice != ''A'' ) && ( aChoice != ''a'' ) && ( aChoice != ''P'' ) && ( aChoice != ''p'' ) )
{
    // Code as before in here...
}



现在,尽管这段代码还可以,但有点令人费解.读时cross着眼睛,我的眼睛.您可能希望查看将条件语句隐藏在其他函数中,以使其更易于阅读.您可以查看的另一种技术可能是跳转表,但这对于此类问题可能是过大的了.

干杯,

Ash



Now while this code is okay, it''s a bit of a bugger to read. My eyes when crosseyed while reading it. You might want to look at hiding the conditionals in other functions to make it easier to read. Another technique you could look at might be a jump table but that''s probably overkill for this sort of problem.

Cheers,

Ash


要执行所需的操作,您需要将上面的代码放入方法中,并在要循环时让方法本身进行调用.这称为递归.

我会问这实际上是否必要,您最好通过执行do/while或while循环来更好.
To do what you want, you need to put your above code into a method, and have the method call itself when want to loop. This is called recursion.

I''d question the need for this practically, you''d be much better off with a do/while or while loop in all probability.


这篇关于如何循环一个if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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