2017-04-11 61 views
-1

我試圖讓一個程序,改變顏色,但它只是不起作用。 我按任何按鈕,它不會改變,然後關閉。我不知道爲什麼它doesent工作爲什麼我的程序改變顏色不起作用?批

@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 

回答

1
@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 
+2

而不是給新的代碼,你實際上應該回答這個問題,並解釋原因現有的代碼不起作用。 – SomethingDark

+1

我沒有看到顯示這種類型的代碼的重點。它使用了一種沒有人使用的語法,當然會混淆批處理文件初學者,就像OP ... **':('** – Aacini

+0

你應該在我的回答中解釋你downvote的原因(除了_revenge downvote_) ,就像我在這裏做的那樣...... **':/'** – Aacini

5

有幾件事情怎麼回事。

最大的問題是您的set報表中:a:f是完全錯誤的。

  • 當您編寫set語句時,只使用一個=符號。
  • 當你說set %letter2%=c,你沒有將letter2的值設置爲c,而是說「將值爲的字符2的值設置爲」。
  • 刪除=符號兩邊的空格;批處理允許空格成爲變量名稱的一部分,因此您創建了一個名爲%letterr2 %的變量並將其值設置爲c
  • 您犯了一個錯字,稱爲變量%字母r 2%而不是%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 
3

如果你想知道爲什麼你的代碼不能正常工作,請參閱@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 onethis one,或this one,或者......

相關問題