我最近做了這個小代碼。
@echo off
mode 1000
set /A x=0
:loop
timeout /T 1 > Nul /nobreak
set /A x=x 1
echo %X%
goto loop
有人可以幫我編輯這段代碼,讓它回圈,直到按下任何鍵然后停止回圈?我是使用批處理編碼的新手,因此不知道該怎么做:(
uj5u.com熱心網友回復:
我認為洗掉 /nobreak 應該會有所幫助
uj5u.com熱心網友回復:
在其他活動正在進行時等待按鍵在批處理腳本中并不是那么簡單。
然而,這是一個不完美但非常簡單的半解決方案,使用choiceand ErrorLevel:
@echo off
set /A "X=0"
:LOOP
rem /* `choice is utilised; all (case-insensitive) letter and numeral keys (excluding
rem key `0`) are defined to quit the loop; `choice` features a timeout, upon which
rem the default choice (key `0` here) is taken: */
choice /C abcdefghijklmnopqrstuvwxyz1234567890 /T 1 /D 0 > nul
rem /* `choice` reflects the actually pressed key in the `ErrorLevel` value, meaning
rem `ErrorLevel = 1` reflects the first key `a`, and `ErrorLevel = 36` reflects
rem the last one in the list, `0`; since the default choice after the timeout is
rem `0`, pressing the `0` key has got the same effect as not pressing any key: */
if ErrorLevel 1 if not ErrorLevel 36 goto :QUIT
set /A "X =1"
echo %X%
goto :LOOP
:QUIT
uj5u.com熱心網友回復:
這種choice方法(請參閱 Mofi 的回答)也是我的第一個想法,但它與您的“任何關鍵”需求相矛盾。
因此我使用了不同的方法:在后臺啟動另一個實體,它只是等待一個鍵(pause)然后創建一個檔案。
一旦檔案存在,主回圈就會退出。
@echo off
setlocal
start /b cmd /c "pause >nul & break>flag"
del flag >nul 2>&1
:loop
if exist flag goto :done
timeout 1 >nul
set /a count =1
echo %count%
goto :loop
:done
del flag >nul 2>&1
echo interrupted by a key press.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/449695.html
上一篇:用數字填充陣列的對角線元素
下一篇:在Python中重復一個程序
