Computer Hope
Microsoft => Microsoft DOS => Topic started by: batchbandit on August 30, 2019, 04:16:02 PM
-
Is there a code I can use in this case?
I have set up the set /p choice codes, but this code
: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!
)
)