简体   繁体   English

“此时,goto是意外的” .bat

[英]“goto was unexpected at this time” .bat

I'm making a blackjack gambling simuator using .bat files. 我正在使用.bat文件制作二十一点赌博模拟器。 At one point i get the error "goto was unexpected at this time". 在某一时刻,我收到错误“此时转到意外”。 Anyone know a solution to this problem? 有人知道解决这个问题的方法吗? I have included the section of code that is causing the problem. 我已经包括了导致问题的代码部分。

    Echo if %%DealerHand%% GTR 21 set DealerStatus=Bust
    Echo if %%DealerHand%%==21 set DealerStatus=Blackjack!
    Echo if %%DealerHand%% LSS 21 set DealerStatus=%%DealerHand%%
    Echo if %%handP4%% GTR 21 set P4status=Bust
    Echo if %%handP4%%==21 set P4status=Blackjack!
    Echo if %%handP4%% LSS 21 set P4status=%%handP4%%
    Echo cls
    Echo Echo Table Number:1
    Echo Echo Dealer:David
    Echo Echo Money:$%%money%%
    Echo Echo Money on table:$%%bet%%
    Echo Echo Players:
    Echo Echo.
    Echo Echo Dealer:Hand:%%DealerStatus%%
    Echo Echo.
    Echo Echo %acctname%:Hand:%%P4status%%
    Echo Echo.
    Echo Echo Enter to continue
    Echo Pause^>nul
    Echo if %%DealerStatus%% == Bust goto winCheck1
    Echo if %%DealerStatus%% == Blackjack! goto DealerWins1
    Echo if %%DealerStatus%% == %%DealerHand%% goto winCheck1
    Echo :winCheck1
    Echo if %%P4hand%% == Bust goto DealerWins1
    Echo if %%P4hand%% == Blackjack! goto P4wins1
    Echo if %%P4hand%% GTR %%DealerHand%% goto P4win1
    Echo if %%P4hand%% LSS %%DealerHand%% goto DealerWins1
    Echo if %%P4hand%% == %%DealerHand%% goto DealerWins1

*PS I'm using this .bat file to generate a new .bat file contianing code. * PS我正在使用此.bat文件生成新的.bat文件包含代码。 Hence the Echo before every line. 因此,每一行之前都有回声。

Any help is good help! 任何帮助都是好的帮助!

Thanks. 谢谢。

Your test for the string "Blackjack!" 您对字符串“二十一点!”的测试 is going to fail due to the exclamation mark. 将由于感叹号而失败。 Try wrapping the test with quotes: 尝试将测试用引号引起来:

Echo if "%%DealerStatus%%" == "Blackjack!" goto DealerWins1

and

Echo if "%%P4hand%%" == "Blackjack!" goto P4wins1

I suspect however, that there are a few other issues in your code that may be tripping you up. 但是,我怀疑您的代码中还有其他一些问题可能会绊倒您。 The %P4hand% variable is a number earlier in your code snippet. %P4hand%变量是代码段中较早的数字。 Are you intending to test %P4status% instead? 您是否打算测试%P4status% You may also want to check your use of tag names for consistency as there's P4wins1 as well as P4win1 in use. 您可能还需要检查标记名称的使用是否一致,因为P4win1使用P4wins1P4win1

The first advice I'll give you is to put ECHO ON at the top of your file (or just remove ECHO OFF if it's there), run it again, and figure out which line has the error. 我给您的第一个建议是将ECHO ON放在文件的顶部(或者,如果有ECHO OFF则将其删除),再次运行它,找出哪一行有错误。 Batch wants to tell you what's wrong, but everybody always suppresses echo even while debugging. Batch想告诉您出了什么问题,但是即使在调试时,每个人也总是抑制回声。

That said, even without knowing which line has the error, my psychic debugging skills tells me that somehow %DealerHand% is the empty string, which causes a syntax error in one of these two lines: 就是说,即使不知道哪一行有错误,我的心理调试技巧%DealerHand%告诉我%DealerHand%是空字符串,这会在这两行之一中引起语法错误:

if %%DealerStatus%% == %%DealerHand%% goto winCheck1

if %%P4hand%% == %%DealerHand%% goto DealerWins1

Running that if statement with %DealerHand% undefined or the empty string will give you the error you see. 运行带有%DealerHand% undefined或空字符串的if语句将给您看到的错误。 You don't show where DealerHand is set, but if it should not be empty, you'll need to make sure it has a value when you enter this code. 您没有显示在DealerHand设置DealerHand ,但是如果它不为空,则在输入此代码时需要确保它具有值。

If it is possible that DealerHand has an empty value, then you can guard against the syntax error by wrapping the variable in some characters, so that both sides of the IF always have non-empty strings. 如果DealerHand的值可能为空,则可以通过将变量包装在某些字符中来防止语法错误,以便IF的两边始终具有非空字符串。

if {%%DealerStatus%%} == {%%DealerHand%%} goto winCheck1

Which evaluates to if {} == {} goto winCheck1 when both variables are null. 当两个变量都为null时,计算结果为if {} == {} goto winCheck1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM