If statement running even when not true

后端 未结 3 1625
南方客
南方客 2021-01-29 13:35

I am not sure what I\'m doing wrong here, every time I run it, it goes through the if part even when it\'s not true? So the \'else\' never runs.

#in         


        
3条回答
  •  长情又很酷
    2021-01-29 14:19

    The problem is your if Statement:

    if (choice == "F" or "f"){
    

    basically, what you say here is: If choise is "F" or if "f". You need to understand: True is everything besides zero (0). "f" is NOT zero, so it is true. You could also wirte (or = ||):

    if (coice == "F" || true)
    

    which is the same like:

    if (true)
    

    So in order for your code to work, you need:

    if (choice == "f" || choice == "F")
    

    That would do what you expect.

提交回复
热议问题