问题描述
早上好,下面的
是我正在批量执行的代码。要求之一是在代码中有一个while循环。但是,这个过程没有在课堂上向我们解释。我还包括了该程序的说明。不确定如何为此编写逻辑。希望你们能有所帮助。感谢您的时间。
在命令提示符下使用Windows Batch。
-使用if和while()命令执行除以0的操作,并进行错误捕获,
Good morning,below is a code I'm doing in batch. One of the requirements is to have a while loop in the code. However, this process was not explained to us in class. I have also included the instructions given for the program. Not sure how to write the logic for this one. Hopefully you guys can help. Thank you for your time.Using Windows Batch in command prompt. - Implement division by 0, with error trapping, using if and while() commands,
操作方法:
-附加名称,当前日期和时间到一个名为results.txt的新文件中。
-使用while循环。
-如果用户为第一个或第二个数字输入值55,必须
终止除以0的实现。
-要求用户输入第一个数字并检查其值。
-要求用户输入第二个数字。
-如果第二个数字为零,则通知用户并要求输入正确的第二个数字。
-如果第二个数字不是零,则进行除法,显示所有数字在计算中使用标签,并将它们添加到result.txt文件中。
How:
- Append your name, current date and time to a NEW file called results.txt
- Use a while loop.
- If the user enters the value 55 for the first or the second number, you must terminate this implementation of divide by 0.
- Ask the user to enter the first number and check its value.
- Ask the user to enter the second number.
- If the second number is ZERO inform the user and ask for a correct second number.
- If the second number is NOT a zero, do the division, display all numbers in this computation using labels, and add them to the results.txt file.
@ echo off
setlocal enabledelayedexpansion
echo results.txt
echo Roberto Martinez >> results.txt
set dt=%date:~7,2%-%date:~4,2%-%date:~10,4%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %dt% >> results.txt
goto :main
:main
setlocal
set /p choice1="Enter your first number"
set /p choice2="Enter your second number"
if !choice1! == 55 goto :eof
if !choice2! == 0 goto :eof
:loop
goto :loop
echo.
echoo ^^
endlocal
推荐答案
首先,StackOverflow的政策是不为您做作业,因此,我仅向您简要介绍您的需要。
First of all, StackOverflow has the policy of not making your homework for you, so I'll only give you a general idea of what you need.
您已经有了goto,并且批次中有标签。在实施while循环时,可以这样使用:
You already have a goto, and a label inside your batch. When implementing a while loop, you can use these like this:
@echo off
:loop
set /p "test=something other than 0 "
if "%test%"=="0" goto :loop
这可以与 while(input == 0)
循环进行比较,因为批处理没有实际的while命令。
This can be compared to a while (input == 0)
loop, as batch has no actual while command.
此外,要划分某些内容,您可以使用类似以下的方法:
Also, to divide something, you can use something like this:
@echo off
set five=5
set /a result=10/%five%
echo %result%
pause
注意设置后的
/ a
Note the /a
after set
这篇关于如何在批处理中进行while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!