الدفعة / ويندوز: أرقام عشوائية
-
05-07-2019 - |
سؤال
وأنا تطوير لعبة بطاقة للدفعة. [سيعطي الارتباط عند القيام]
وBasiclly ما أريد القيام به هو 'مجموعة' '٪٪ عشوائية' إلى '٪ يخدر٪.
set %numbs%=%random%
وبعد ذلك أريد المستخدم لكتابة قيمة واحدة من أرقام عشوائية٪٪ عرض
echo %numbs%
Set nuchoo=
set /p nuchoo=Number:
وماذا عن سؤالي الآن هو حول كيفية التحقق إذا كانت قيمة كتبته في من قبل المستخدم هو في المجلد٪ يخدر قيمة٪. إذا كان كذلك،
GOTO :validnucho
وإذا لم يكن كذلك،
GOTO :unvalidnucho
وعندما يذهب إلى: validnucho، ينبغي أن حذف هذا العدد من٪ يخدر٪ فار. لا يهم إذا كان من الواضح٪ يخدر٪ فار يحتوي على 2X عدد، لذلك فإنه يجب حذف واحد من اثنين فقط. بقية وأنا أعلم.
المحلول
وهذا يجب ان تحصل على انك بدأته
حفظ ما يلي Instr.cmd
@echo off
rem These two lines allow tracing to be turned on by entering
rem set TRACE=ECHO on the cmd line
if not defined TRACE set TRACE=rem
%TRACE% on
rem Allow use of delayed variable expansion using !
setlocal ENABLEDELAYEDEXPANSION
if /I [%1]==[Test] (
call :Test
goto :eof
)
:Instr
set mTestStr=%1
rem set /a does arithmatic
set /a TestIdx=0
set Found=N
set CopiedStr=
:startLoop
rem extract character at index TestIndx to test
set TestedChar=!mTestStr:~%TestIdx%,1!
rem echo TestedChar if trace is on
%TRACE% %TestedChar%
If [!TestedChar!]==[] (
goto :exitInstr
)
If [%TestedChar%]==[%2] (
rem Testedchar matchs search character,
rem so we have found the char we were looking for
if %Found%==Y (
set CopiedStr=%CopiedStr%%TestedChar%
)
set Found=Y
) else (
set CopiedStr=%CopiedStr%%TestedChar%
)
rem Increment Index
SET /a TestIdx=TestIdx+1
goto :startLoop
:exitInstr
rem endlocal but keep CopiedStr and Found
endlocal & set CopiedStr=%CopiedStr% & set Found=%Found%
goto :eof
:Test
call :Instr abcde c
call :Verify Y %Found% "call :Instr abcde c - Found"
call :Verify abde, %CopiedStr% "call :Instr abcde c - CopiedStr"
call :Instr abcde x
call :Verify N %Found% "call :Instr abcde x - Found"
call :Verify abcde, %CopiedStr% "call :Instr abcde x - CopiedStr"
call :Instr abcdec c
call :Verify Y %Found% "call :Instr abcdec c - Found"
call :Verify abdec, %CopiedStr% "call :Instr abcdec c - CopiedStr"
echo Tests Done
goto :eof
:Verify
set Expected=%1
set Expected=%Expected:"=%
set Actual=%2
set Actual=%Actual:"=%
set Msg=%3
set Msg=%msg:"=%
If not "%Expected%"=="%Actual%" (
echo Failed: Expected "%Expected%" but was "%Actual%" - %Msg%
) else (
echo OK Expected "%Expected%" - %Msg%
)
goto :eof
لا تنتمي إلى StackOverflow