值a1到a9等于某个数字,即模数13(0-12),值0到12则等于卡范围2-A。我试图通过以下代码从a3中调用A或从a1调用7中的7:
for %%R in (1 3) do (for %%S in (!a%%R!) do if !b%%R!==4 echo P1 has a !%%S! 4 of a kind
if !b%%R!==3 echo P1 has a !%%S! 3 of a kind
if !b%%R!==2 echo P1 has a !%%S! pair)
for %%T in (2 4) do (for %%U in (!a%%T!) do if !b%%T!==4 echo CP has a !%%U! 4 of a kind
if !b%%T!==3 echo CP has a !%%U! 3 of a kind
if !b%%T!==2 echo CP has a !%%U! pair)
但输出只是" P1有一对"或者" CP有3种类型的#34;没有我需要的价值。 b1到b4并不重要,它们只是表示要识别对的重复卡的数量等。
我如何收到我想要的输出,以及如何简化这一点:
set 0=A
set 1=2
set 2=3
set 3=4
set 4=5
set 5=6
set 6=7
set 7=8
set 8=9
set 9=10
set 10=J
set 11=Q
set 12=K
答案 0 :(得分:1)
缩短设定命令的顺序 -
setLocal enableDelayedExpansion
set i=0
for %%a in (A 1 2 3 4 5 6 7 8 9 10 J Q K) do (
set !i!=%%a
set /a i+=1
)
答案 1 :(得分:1)
您的代码有几个问题。首先,第二个for
命令没有被适当地嵌套,因为它仅适用于第一个if
命令。这样,在第二个和第三个if
命令中,%%S
可替换参数未定义。编写代码的方式很难将这类错误隔离开来,因此我的第一个建议是始终在嵌套命令中使用对齐,并以一种清楚标记每个for
或{的结尾的方式放置右括号。 {1}}构造。下面是这种格式的一个例子。
排在第二位,您的代码会产生以下行为:
if
在最后一行%%R = 1
%%S = !a%%R! = !a1! = a numeric value between 0 and 12
if !b1!==4 echo P1 has a !%%S! 4 of a kind
有一个数值,因此%%S
会尝试替换名称为数字的变量的值!我想你在这里忘记了一个数组,所以我插入!%%S!
作为存储卡名称的数组的名称,如上所示:
card
输出示例:
@echo off
setlocal EnableDelayedExpansion
rem Define names of cards
set i=0
for %%a in (A 2 3 4 5 6 7 8 9 10 J Q K) do (
set card!i!=%%a
set /A i+=1
)
rem The values a1 through a9 are equal to some number that is modulus 13 (0-12)
for /L %%i in (1,1,9) do set /A a%%i=!random! %% 13
rem b1 through b4 ... signify the number of repeated cards there are to identify pairs and such.
set "b1=2" // two cards of b1 type
set "b2=0" // no cards of b2 type
set "b3=4" // four cards of b3 type
set "b4=1" // one card of b4 type
rem I am trying to call A from 0 from a3 or the 7 from 8 from a1 with this code:
for %%R in (1 3) do for %%S in (!a%%R!) do (
if !b%%R!==4 echo P1 has a !card%%S! 4 of a kind
if !b%%R!==3 echo P1 has a !card%%S! 3 of a kind
if !b%%R!==2 echo P1 has a !card%%S! pair
)
for %%T in (2 4) do for %%U in (!a%%T!) do (
if !b%%T!==4 echo CP has a !card%%U! 4 of a kind
if !b%%T!==3 echo CP has a !card%%U! 3 of a kind
if !b%%T!==2 echo CP has a !card%%U! pair
)
您也可以通过这种方式进行进一步的简化:
C:\> test
P1 has a A pair
P1 has a Q 4 of a kind
在最后的简化中,您还可以定义一个“倍数名称”数组,如“2 =对”,“3 = 3类”等,如果您想要显示更精确的结果。
答案 2 :(得分:0)
我无法保证代码的逻辑。但是你在每个内部FOR循环中都缺少一组括号。按照目前的编写,%%S
和%%U
仅为每个IF语句定义。你需要它们三个语句,如:
for %%R in (1 3) do (
for %%S in (!a%%R!) do (
if !b%%R!==4 echo P1 has a !%%S! 4 of a kind
if !b%%R!==3 echo P1 has a !%%S! 3 of a kind
if !b%%R!==2 echo P1 has a !%%S! pair
)
)
for %%T in (2 4) do (
for %%U in (!a%%T!) do (
if !b%%T!==4 echo CP has a !%%U! 4 of a kind
if !b%%T!==3 echo CP has a !%%U! 3 of a kind
if !b%%T!==2 echo CP has a !%%U! pair
)
)
或者您可以按如下方式缩短它:
for %%R in (1 3) do for %%S in (!a%%R!) do (
if !b%%R!==4 echo P1 has a !%%S! 4 of a kind
if !b%%R!==3 echo P1 has a !%%S! 3 of a kind
if !b%%R!==2 echo P1 has a !%%S! pair
)
for %%T in (2 4) do for %%U in (!a%%T!) do (
if !b%%T!==4 echo CP has a !%%U! 4 of a kind
if !b%%T!==3 echo CP has a !%%U! 3 of a kind
if !b%%T!==2 echo CP has a !%%U! pair
)
请注意,无需不断更改用于每个独立FOR块的字母。使用
也可以for %%R in (1 3) do for %%S in (!a%%R!) do (
if !b%%R!==4 echo P1 has a !%%S! 4 of a kind
if !b%%R!==3 echo P1 has a !%%S! 3 of a kind
if !b%%R!==2 echo P1 has a !%%S! pair
)
for %%R in (2 4) do for %%S in (!a%%R!) do (
if !b%%R!==4 echo CP has a !%%S! 4 of a kind
if !b%%R!==3 echo CP has a !%%S! 3 of a kind
if !b%%R!==2 echo CP has a !%%S! pair
)