【问题标题】:why does my program that changes color not work? Batch为什么我的改变颜色的程序不起作用?批
【发布时间】:2017-04-11 11:28:30
【问题描述】:

我试图制作一个改变颜色的程序,但它不起作用。 我按下任何按钮,它不会改变,然后就关闭了。我不知道为什么它不起作用

@echo off
set letter2=0
:1
set color=%random%
if %color% LSS 10 goto next
goto 1
:next
set letter=%random%
if %random% LSS 6 goto 2
:2 
if %letter% == 0 goto A
if %letter% == 1 goto B
if %letter% == 2 goto C
if %letter% == 3 goto D
if %letter% == 4 goto E
if %letter% == 5 goto F
goto next
:a
set %letterr2% == a
goto final
:b
set %letterr2% == b
goto final
:c
set %letterr2% == c
goto final
:d
set %letterr2% == d
goto final
:e
set %letterr2% == e
goto final
:f
set %letterr2% == f
:final
set realcolor=%letter2%+%color%
cls
color %realcolor%
echo hey this color is %color%
pause>nul
goto 1

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    这里发生了一些事情。

    最大的问题是你在:a:f中的set语句是完全错误的。

    • 在编写 set 语句时,仅使用一个 = 符号。
    • 当您说 set %letter2%=c 时,您并不是将 letter2 的值设置为 c,而是说“将 letter2 的值是什么 的值设置为 c”。
    • 去掉=符号两边的空格; batch 允许空格作为变量名的一部分,因此您创建了一个名为 %letterr2 % 的变量并将其值设置为 c
    • 您打错了字,将变量命名为 %letterr2% 而不是 %letter2%。

    字符串连接不需要+,只需将两个变量贴在一起即可。

    您的脚本需要永远运行,因为 %random% 返回一个介于 1 和 32768 之间的数字。它低于 10 的几率微乎其微。它低于 6 的几率甚至更小。如果您想要一个介于 1 和 n 之间的随机数,请使用代码 set /a number=%random% %% n

    最终,您的代码将如下所示:

    @echo off
    set letter2=0
    :1
    set /a color=%random%%%10
    set /a letter=%random%%%6
    
    if %letter% == 0 goto A
    if %letter% == 1 goto B
    if %letter% == 2 goto C
    if %letter% == 3 goto D
    if %letter% == 4 goto E
    if %letter% == 5 goto F
    
    :a
    set letter2=a
    goto final
    :b
    set letter2=b
    goto final
    :c
    set letter2=c
    goto final
    :d
    set letter2=d
    goto final
    :e
    set letter2=e
    goto final
    :f
    set letter2=f
    :final
    set realcolor=%letter2%%color%
    cls
    color %realcolor%
    echo hey this color is %color%
    pause>nul
    goto 1
    

    【讨论】:

      【解决方案2】:

      如果您想知道为什么您的代码不起作用,请查看@SomethingDark 的extensive explanation

      但是,如果您想看到一种更简单的方法来做同样的事情,那么您可以查看以下代码:

      @echo off
      setlocal EnableDelayedExpansion
      
      set letters=abcdef
      
      :loop
      set /A color=%random% %% 10
      set /A letter=%random% %% 6
      
      set letter2=!letters:~%letter%,1!
      
      set realcolor=%letter2%%color%
      cls
      color %realcolor%
      echo hey this color is %realcolor%
      pause>nul
      goto loop
      

      此代码中的关键点是延迟扩展的使用,因此我建议您在此站点中搜索这样一个术语,其中有大量相关答案,例如this one,或this one,或this one,或this one,或...

      【讨论】:

        【解决方案3】:
        @echo off
        
        :loop
            call :getRandom
            cls
            color %randomColor%
            echo This color is %randomColor%
            pause >nul
        goto :loop
        
        :getRandom
            set "randomColor="
            :getRandomLoop
                set /a "color=%random% %% 15"
                set "if=if %color%=="
                set "then=set color="
                    %if%10 %then%A
                    %if%11 %then%B
                    %if%12 %then%C
                    %if%13 %then%D
                    %if%14 %then%E
                    %if%15 %then%F
                set "randomColor=%randomColor%%color%"
                if "%randomColor:~1,1%"=="" goto :getRandomLoop
        goto :EOF
        

        【讨论】:

        • 您应该实际回答问题并解释为什么现有代码不起作用,而不是提供新代码。
        • 我看不出显示此类代码的意义。它使用了一种没人使用的语法,这肯定会让批处理文件初学者感到困惑,比如 OP... :(
        • 你应该在我的回答中解释你投反对票的原因(除了revenge downvote),就像我在这里所做的那样...... :/
        猜你喜欢
        • 2023-03-19
        • 2020-10-13
        • 2015-04-08
        • 2022-08-23
        • 2021-09-27
        • 1970-01-01
        • 2013-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多