Why doesn't this work....

xdylanpc

New member
Joined
Jun 18, 2023
Messages
2
Programming Experience
Beginner
If the player doesn't pick up the ak47 it still prints out that they win.
C#:
    public static void Main()
    {
        Console.WriteLine("Welcome to my horror game: the super death scary monster of doooom");

        Console.WriteLine("Once upon a time, a scary monster \n Yes or no?");
        string userInput1 = Console.ReadLine();

       

        bool ak47;      
            if (userInput1 == "Yes" || userInput1 == "yes")
            {
                Console.WriteLine("You picked up the ak47!");
                ak47 = true;
           
            }        
            else
            {
                Console.WriteLine("You didn't pick up the ak47");
                ak47 = false;              
            }

            Console.WriteLine("THE SUPER DEATH SCARY MONSTER OF DOOOOM IS ATTACKING YOUR CHICKENS!!! do you attack??");
            string input2 = Console.ReadLine();
           

            if (input2 == "yes" || input2 == "Yes" && ak47 == true)
            {
                Console.WriteLine("you whip out the blicky and blast that maamajaama in his scary monster balls");
                Console.WriteLine("you win!");
               
            }
            else if (input2 == "yes" || input2 == "Yes" && ak47 == false)
            {
                Console.WriteLine("The super death scary monster of doom deflects your attack using wing chun and curses you with death doom and you die");             
            }
            else
            {
                Console.WriteLine("yur chikens r dead");              
            }
    }
}
 
Last edited by a moderator:
Welcome to the forums. In the future, please post your code in code tags. It's the button on the toolbar that looks like </>.

The && has higher precedence than ||. So this on line 27:
C#:
input2 == "yes" || input2 == "Yes" && ak47 == true
is interpreted as:
C#:
input2 == "yes" || (input2 == "Yes" && ak47 == true)

So if input2 is "yes", the playe wins regardless of the ak47 being true or false.

 
Welcome to the forums. In the future, please post your code in code tags. It's the button on the toolbar that looks like </>.

The && has higher precedence than ||. So this on line 27:
C#:
input2 == "yes" || input2 == "Yes" && ak47 == true
is interpreted as:
C#:
input2 == "yes" || (input2 == "Yes" && ak47 == true)

So if input2 is "yes", the playe wins regardless of the ak47 being true or false.


Welcome to the forums. In the future, please post your code in code tags. It's the button on the toolbar that looks like </>.

The && has higher precedence than ||. So this on line 27:
C#:
input2 == "yes" || input2 == "Yes" && ak47 == true
is interpreted as:
C#:
input2 == "yes" || (input2 == "Yes" && ak47 == true)

So if input2 is "yes", the playe wins regardless of the ak47 being true or false.



So I need to put ak47 && input2 == yes || input 2 ==
Welcome to the forums. In the future, please post your code in code tags. It's the button on the toolbar that looks like </>.

The && has higher precedence than ||. So this on line 27:
C#:
input2 == "yes" || input2 == "Yes" && ak47 == true
is interpreted as:
C#:
input2 == "yes" || (input2 == "Yes" && ak47 == true)

So if input2 is "yes", the playe wins regardless of the ak47 being true or false.


Thank you!
 
Welcome to the forums. In the future, please post your code in code tags. It's the button on the toolbar that looks like </>.

The && has higher precedence than ||. So this on line 27:
C#:
input2 == "yes" || input2 == "Yes" && ak47 == true
is interpreted as:
C#:
input2 == "yes" || (input2 == "Yes" && ak47 == true)

So if input2 is "yes", the playe wins regardless of the ak47 being true or false.


You're short-circuiting your input conditional.
You need to get used to using parenthesis around your logic otherwise order of ops is going to screw you up.

Think of it this way:

AND/OR:
bool1 = false;
bool2 = true;
bool3 = true;

IF (bool1 || bool2 && bool3)
{
    //what would this do?
}

Now, write it this way:

IF ((bool1 || bool2) && bool3)
{
    //now, what would THIS do?
}

 
So I need to put ak47 && input2 == yes || input 2 ==

No, you need to use parentheses to make your intentions clear both to the compiler and to other people who read your code after you do

Always use parentheses; over-use them even.. Visual Studio will draw them in a dim color if they can be removed

At any time that you want to mix OR and AND, always, always, always use parentheses. Always.
 
No, you need to use parentheses to make your intentions clear both to the compiler and to other people who read your code after you do

Always use parentheses; over-use them even.. Visual Studio will draw them in a dim color if they can be removed

At any time that you want to mix OR and AND, always, always, always use parentheses. Always.

Yes GOD! SECONDED!
You would be surprised how often a missing parenthesis can screw up logic.
 

Latest posts

Back
Top Bottom