批量文件不附加,如果语句无法正常工作

时间:2017-04-02 22:27:51

标签: batch-file cmd

我在结束时试图弄清楚我的批处理文件有什么问题

我似乎无法使某些语句起作用,并且最终的echo语句不会附加到文档中。有人可以加入吗?

如果用户输入66

,我希望第一个num终止

如果用户输入零

,我还希望第二个数字跳转到ZeroError

我无法弄清楚为什么它不会在最后显示平均值或创建one.txt文件

@echo off



setlocal ENABLEDELAYEDEXPANSION


type NUL > results.txt

:MAIN
echo My Name is ****** > results.txt
echo The Current date is  %date% >> results.txt
echo The Current time is %time% >> results.txt
echo My name is ****, the current date is %date%  and the current time is %time%

:FirstNum



SET /P FirstNumber=Enter the First Number and press Enter :
if %FirstNumber% == "66" goto LOOP


:SecondNum

SET /P LastNumber=Enter the Second Number and press Enter: 
if %LastNumber% == "0" goto ZeroError
if %LastNumber% == "66" goto LOOP
goto CALC


:ZeroError
SET /P zError =You CAN NOT divide by Zero, enter a correct number and press Enter:
if %LastNumber% == "66" goto LOOP
if %LastNumber% == "0" goto ZeroError
goto CALC

:CALC



set /A calculation = %FirstNumber% / %LastNumber%
echo %FirstNumber% divided by %LastNumber% equals %calculation%
echo %FirstNumber% divided by %LastNumber% equals %calculation% >> results.txt




pause


:LOOP

FOR /L %%A IN (1,1,10) DO(

SET /A  MOD= %%A %% 6
SET /A MODPlus= !MOD! + 2
SET /A TOTAL  += !MODPlus!
SET /A AVERAGE = !TOTAL!/10

)

echo The mean of the values is %AVERAGE% >> results.txt

mkdir pgm4
cd pgm4
type NUL > one.txt
echo REM ***** >> one.txt
echo echo ****** >> one.txt
ECHO. >> one.txt
  • 在我的FirstNum标签中,尽管我在提示符中输入了66,但程序仍在继续

  • SecondNum中的类似问题

  • 之后的回显:LOOP不会将平均值附加到文本文件中。

有人能指出我做错的方向吗?

1 个答案:

答案 0 :(得分:0)

你似乎需要用勺子喂叹息,

  • 要比较相等,如果用户输入"66"并且只有命令if %FirstNumber% == "66" goto LOOP可以评估为真,则双方需要相等
  • 过多的空格不会让您的代码更易于阅读。
  • set /a计算可以合并为一行,对于变量不需要%!(除了变量)。
echo off
setlocal ENABLEDELAYEDEXPANSION

:MAIN
( echo My Name is ****** 
  echo The Current date is  %date%
  echo The Current time is %time%
) >result.txt 
echo My name is ****, the current date is %date%  and the current time is %time%

:FirstNum
SET /P FirstNumber=Enter the First Number and press Enter :
if "%FirstNumber%"=="66" goto LOOP

:SecondNum
SET /P LastNumber=Enter the Second Number and press Enter: 
if "%LastNumber%"=="0" goto ZeroError
if "%LastNumber%"=="66" goto LOOP
goto CALC

:ZeroError
SET /P zError =You CAN NOT divide by Zero, enter a correct number and press Enter:
if "%LastNumber%"=="66" goto LOOP
if "%LastNumber%"=="0" goto ZeroError
goto CALC

:CALC
set /A calculation=FirstNumber/LastNumber
echo %FirstNumber% divided by %LastNumber% equals %calculation%
echo %FirstNumber% divided by %LastNumber% equals %calculation% >> results.txt
pause

:LOOP
FOR /L %%A IN (1,1,10) DO (
  SET /A  "MOD= %%A %% 6, MODPlus=MOD + 2, TOTAL+=MODPlus, AVERAGE=TOTAL/10"
)
echo The mean of the values is %AVERAGE% >> results.txt
mkdir pgm4
cd pgm4

( echo REM *****
  echo echo ******
  echo.
) > one.txt