第一个脚本:选择选择和IF语句时出错

时间:2018-07-22 04:46:29

标签: batch-file

这是我的代码,如果您想自己运行它,只需将其复制并另存为.bat。 它的意思是成为剪刀石头纸,看看我是否可以批量进行游戏,在您选择剪刀石头纸后,它会一直关闭。

@echo off  
:setting  
title Scissors, Paper, Rock  

`Choosing between Scissors, Paper and Rock`
:an  
set /a ran=%random%  
if /i %ran% GTR 3 goto an  
if /i %ran% LSS 1 goto an  
if %ran%==1 set %ans%=Scissors  
if %ran%==2 set %ans%=Paper  
if %ran%==3 set %ans%=Rock  

`Human Making a Choice`
:in  
echo Choose:  
echo [1] = Scissors  
echo [2] = Paper  
echo [3] = Rock  
set /p text=  
if text==1 goto 1  
else if text==2 goto 2  
else if text==3 goto 3  

`Outcome`
:1  
echo You chose Scissors. The Computer chooses %ans%  
if %ans%==Scissors echo Draw  
if %ans%==Paper echo Win  
if %ans%==Rock echo Loose  
pause  
goto out  

`Outcome`
:2  
echo You chose Paper. The Computer chooses %ans%  
if %ans%==Scissors echo Loose  
if %ans%==Paper echo Draw  
if %ans%==Rock echo Win  
pause  
goto out  

`Outcome`
:3  
echo You chose Rock. The Computer chooses %ans%  
if %ans%==Scissors echo Win  
if %ans%==Paper echo Loose  
if %ans%==Rock echo Draw  
pause  
goto out  

`Play again or not`
:out  
echo GG Want to play again?  
echo [1] = Yes  
echo [2] = No  
set /p text=  
if text==1 goto setting  
if text==2 pause  

谢谢大家的帮助,因为我现在真的很困惑。

3 个答案:

答案 0 :(得分:1)

  • 使用choice.exe,您可以保存一些输入验证,因为它可以防止非法输入。
  • set /a允许在一个命令中进行多次计算,并且不需要在%/!中包含vars
  • 批处理循环(具有超时延迟)并保持得分。
  • net user获取用户的名字

:: Q:\Test\2018\07\22\SO_51462128.cmd
@Echo off&SetLocal EnableDelayedExpansion
title Scissors, Paper, Rock

:: build array of ans[] 
Set "ans[1]=Scissors"&Set "ans[2]=Paper"&Set "ans[3]=Rock"
Set /A "wins=Draw=Loss=0"

:: Get User first name
For /f "tokens=1-3,*" %%A in (
  'net user "%USERNAME%" ^| findstr /i /B "..ll"'
) Do set "FirstName=%%C"

:restart
cls
Echo Hi %FirstName%, let's play.                %Score%

REM Choosing between Scissors, Paper and Rock
SET /A "min=1,max=3,Comp=(%RANDOM% %% max)+min"
Set "ans=!ans[%Comp%]!"

REM Human Making a Choice
echo Choose:
echo [1] = Scissors
echo [2] = Paper
echo [3] = Rock
Choice /CS /C 123e /M "e to exit"
set "User=%ErrorLevel%"
If %User%==4 exit /B
echo You chose !ans[%User%]!. The Computer chooses !ans[%Comp%]!
If %User%==%Comp% Call :Score Draw & Goto :ReStart
goto :Outcome%User%

:Outcome1 Scissors
if %ans%==Paper Call :Score Wins & Goto :ReStart
Call :Score Loss & Goto :ReStart

:Outcome2 Paper
if %ans%==Rock Call :Score Wins & Goto :ReStart
Call :Score Loss & Goto :ReStart

:Outcome3 Rock
if %ans%==Scissors Call :Score Wins & Goto :ReStart
Call :Score Loss & Goto :ReStart

:Score
Echo **%1**
set /A "%1+=1"
Set "Score=Wins:[%Wins%] Draw:[%Draw%] Loss:[%Loss%]"
timeout /t 5 >Nul

示例输出:

Hi LotPings, let's play.                Wins:[4] Draw:[4] Loss:[1]
Choose:
[1] = Scissors
[2] = Paper
[3] = Rock
e to exit [1,2,3,e]?

答案 1 :(得分:1)

在计算机程序中,有几种常见的技术可以编写更简单,更短的代码。其中最常用的一种是 array ,因为它允许使用相同的代码使用不同的值,而只是使用正确的值来选择适当的数组元素。您可以在this answer上了解批处理文件中的阵列管理。

下面的代码不是以单独的方式管理每个项目(剪刀,纸或石头),但是以相同的方式管理所有项目,并通过数字选择适当的项目索引下标。该程序使用一个简单的技巧来适应这三个项目的循环特性。仅当对手的项目是岩石时,算术表达式才会将0(意味着“比剪刀少纸”)转换为3(意味着“比岩石大纸”)。

@echo off
setlocal EnableDelayedExpansion
title Scissors, Paper, Rock

rem Define array of items in "natural" (cyclic) order
set "item[0]=Paper"
set "item[1]=Scissors"
set "item[2]=Rock"
set "item[3]=Paper"
set /A "Loss=0, Draw=0, Wins=0"

cls
:loop
echo/

rem Choosing between Scissors, Paper and Rock
set /A "comp=%random% %% 3"

rem Human Making a Choice
choice /N /C PSRe /M "Choose Scissors, Paper or Rock (E to exit): "
if %errorlevel% equ 4 exit /B

set /A "human=%errorlevel%-1, comp+=^!comp*^!(human-2)*3, human+=^!human*^!(comp-2)*3"
echo You chose !item[%human%]!. The Computer chooses !item[%comp%]!.

if %human% gtr %comp% (
   set /A Wins+=1
) else if %human% lss %comp% (
   set /A Loss+=1
) else (
   set /A Draw+=1
)

echo Loss: %Loss%  -  Draw: %Draw%  -  Wins: %Wins%
goto loop

在下面的代码中,不再将三个不同的得分结果(亏损,平局和获胜)作为单独的项目进行管理,而是以与同一score数组的元素相同的方式进行计算。一个非常简单的表达式将用于得分识别的多个if命令替换为02之间的单个值。

@echo off
setlocal EnableDelayedExpansion
title Scissors, Paper, Rock

rem Define array of items in "natural" (cyclic) order
set "item[0]=Paper"
set "item[1]=Scissors"
set "item[2]=Rock"
set "item[3]=Paper"

rem Define the elements of the "score" results array
set /A "score[0]=0, score[1]=0, score[2]=0"

cls
:loop
echo

rem Choosing between Scissors, Paper and Rock
set /A "comp=%random% %% 3"

rem Human Making a Choice
choice /N /C PSRe /M "Choose Scissors, Paper or Rock (E to exit): "
if %errorlevel% equ 4 exit /B

set /A "human=%errorlevel%-1, comp+=^!comp*^!(human-2)*3, human+=^!human*^!(comp-2)*3, result=human-comp+1"
echo You chose !item[%human%]!. The Computer chooses !item[%comp%]!.

set /A "score[%result%]+=1"
echo Loss: %score[0]%  -  Draw: %score[1]%  -  Wins: %score[2]%
goto loop

答案 2 :(得分:0)

Re做了一些代码并添加了备注。

@echo off
:setting
title Scissors, Paper, Rock

REM Choosing between Scissors, Paper and Rock
:an
:: Changed the way you generate the random number as it makes it much quick. This forced the answer to be 1, 2 or 3.
SET maxvalue=3
SET minvalue=1
SET /A ran=((%RANDOM%)%%(%maxvalue%))+(%minvalue%)
:: As you are comparing numbers, use the EQU, not ==. == is for comparing strings, not numerical values
:: When setting a variable, don't use the % sign, just type their name and call it with the % sign.
if %ran% equ 1 set ans=Scissors
if %ran% equ 2 set ans=Paper
if %ran% equ 3 set ans=Rock
if /i %ran% GTR 3 goto :an
if /i %ran% LSS 1 goto :an

REM Human Making a Choice
:in
echo Choose:
echo [1] = Scissors
echo [2] = Paper
echo [3] = Rock
set /p text=
if %text% equ 1 goto :1
if %text% equ 2 goto :2
if %text% equ 3 goto :3

REM Outcome
:1
echo You chose Scissors. The Computer chooses %ans%
if %ans%==Scissors echo Draw
if %ans%==Paper echo Win
if %ans%==Rock echo Loose
pause
goto :out

REM Outcome
:2
echo You chose Paper. The Computer chooses %ans%
if %ans%==Scissors echo Loose
if %ans%==Paper echo Draw
if %ans%==Rock echo Win
pause
goto :out

REM Outcome
:3
echo You chose Rock. The Computer chooses %ans%
if %ans%==Scissors echo Win
if %ans%==Paper echo Loose
if %ans%==Rock echo Draw
pause
goto :out

REM Play again or not
:out
echo GG Want to play again?
echo [1] = Yes
echo [2] = No
set /p Restart=
if %Restart% equ 1 goto :setting
if %Restart% equ 2 pause