用户按Enter键后关闭批处理文件

时间:2016-11-10 05:00:19

标签: batch-file cmd

以下代码应该进行除法计算。它应该要求用户输入第一个数字,等待输入,然后用第二个数字重复。

(一些额外信息:不允许用户输入数字22或0)

到目前为止,在您输入第一个号码后,它就会退出我身上。我不知道为什么。

任何帮助将不胜感激!

@echo off
cls

:START
set /p FN = Enter first number: 
if %FN% == 22 GOTO EXIT
GOTO SECONDNUMBER


:EXIT
exit /b

:SECONDNUMBER
set /p SN = Enter second number: 
if "%SN%" == 22 exit /b
if "%SN%" == 0 GOTO ZEROERROR

GOTO DIVIDE


:ZEROERROR
echo Sorry! You CAN NOT divide by ZERO. Please enter a new number.
pause
cls
GOTO SECONDNUMBER

:DIVIDE
set /a RESULT = %FN%/%SN%
echo %FN% divided by %SN% = %RESULT%
pause
cls
GOTO START

1 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情:

@echo off
Title Division Computation
:START
cls
Color 0A
set /p "FN=Enter first number : "
if "%FN%" EQU "22" Exit
::*******************************
:SECONDNUMBER
Color 0A
set /p "SN=Enter second number : " 
if "%SN%" EQU "22" Exit
if "%SN%" EQU "0" GOTO ZEROERROR
GOTO DIVIDE
::*******************************
:DIVIDE
set /a RESULT=%FN% / %SN%
echo %FN% divided by %SN% = %RESULT%
pause
cls
GOTO START
::*******************************
:ZEROERROR
Color 0C
echo Sorry! You CAN NOT divide by ZERO. Please enter a new number.
pause
cls
GOTO SECONDNUMBER
::*******************************

这是一个额外的 计算器,带有TypeWriter和Speaking Voice 来自我,只是为了好玩:)

@echo off
Title Calculator with a TypeWriter and Speaking Voice by Hackoo 2016
Color 0A & Mode con cols=80 lines=3
Call :TypeWriter "Hello, Welcome to my calculator. First off would you like to tell me your name ? "
echo(
Call :TypeWriter "So, Whats your name ? "
set /p name=
Call :TypeWriter "Ah, Nice to meet you %name% !"
::*************************************************************
:start
Call :TypeWriter "What is the first number you want to use ? "
set /p no=
Call :TypeWriter "What Operation do you want to use ? "
set /p op=
Call :TypeWriter "What is the second number you would like to use ? "
set /p no2=
Call :TypeWriter "%no% %op% %no2% "
set /a ans=%no% %op% %no2%
Call :TypeWriter " Well, That was an easy one the answer is %no% %op% %no2% = %ans% "
Call :TypeWriter " Would you like to do another calculaton ? Y(Yes) or N(No) ?"
set /p Question=
If /I "%Question%"=="Y" (goto :start) else (Goto :end)
::*************************************************************
:TypeWriter
Cls
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 150
echo For x = 1 to intTextLen
echo     strTempText = Mid(strText,x,1^)
echo     WScript.StdOut.Write strTempText
echo     WScript.Sleep intPause
echo Next
echo Set Voice=CreateObject("SAPI.SpVoice"^)
echo voice.speak strText
)>%tmp%\%~n0.vbs
@cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
exit /b
::**************************************************************
:end
Exit
::*************************************************************