|
Answer» Is there a CODE I can use in this case?
I have set up the set /p choice codes, but this code
Code: [Select]:game Player 1 CHOSE %p1choice%! Player 2 chose %choose%! if %p1choice%='1' and %choose%='2' ECHO Player 2 wins! does not work.
The code closes itself. So, I want to know - is there a code I can use that WORKS LIKE the "if [choice=1] and [choice=2] then echo [xyz]"You don't get logical AND operator in batch but you can simulate it with 2 IF tests on one line
You have the syntax wrong for IF tests, you need double equals signs, and if you have quotes they must be on left and right side of the == and the action you take if both tests are passed must be on the same line or in a bracket block
if '%p1choice%'=='1' if '%choose%'=='2' echo Player 2 wins!
if '%p1choice%'=='1' if '%choose%'=='2' ( echo Player 2 wins! )
Study batch syntax at a good site like SS64.com.
Play around at the prompt
C:\>set p=1
C:\>set q=1
C:\>set r=2
C:\>if '%p%'=='1' echo yes yes
C:\>if '%p%'=='1' if '%q%'=='1' echo yes yes
C:\>if '%p%'=='1' if '%q%'=='2' echo yes
C:\>if '%p%'=='1' if '%r%'=='2' echo yes yes
Or you can do this
if '%p1choice%'=='1' ( if '%choose%'=='2' ( echo Player 2 wins! ) )
|