Do-While 循环不循环,因为不管是真还是假,While 都会执行
Do-While loop is not looping as While is executed regardless of whether it is true or false
static double myconversion(string farenheitinput)
{
double temporary = 0;
try
{
double farenheit = double.Parse(farenheitinput);
temporary = farenheit;
}
catch
{
Console.Write("\nWrong! you must write a number");
}
double celsius = (temporary - 32) * 5 / 9;
return celsius;
}
public void Main(string[] args)
{
double celsius = 0;
do
{
Console.Write("\nWrite your temperature in farenheit: ");
string farenheitinput = (Console.ReadLine());
celsius = myconversion(farenheitinput);
if (celsius < 73)
{
Console.WriteLine("\nThat temperature is too cold");
}
else if (celsius > 77)
{
Console.WriteLine("\nThat temperature is too hot");
}
}
while ((celsius >= 73) && (celsius <= 77));
{
Console.WriteLine("\nThe temperature is perfect");
}
Console.Write("\nPress any key to close program...");
Console.ReadKey();
}
我正在使用一种方法将华氏度转换为摄氏度。
这是我用于我的循环和方法的代码,当我调试它时,如果用户输入 190 华氏度,它会显示双摄氏度 = 87.7,这对于 while 来说是一个错误条件,代码应该循环,但它不会t 并进入 while 代码并结束循环。我不知道是什么原因造成的,所以我想我是来这里问的。
循环条件表达式:
循环条件的工作方式是当它们评估为 true
.
时它们继续
运行 0 or more times
而 true
while (boolean expression|reference == true) { // do stuff }
运行 1 or more times
而 true
do { // do stuff } while (boolean expression|reference == true);
在任何一种情况下,第一个 false
遇到循环停止。
解决方案
你做了什么:
do
{
// input stuff
}
while ((73 >= celsius) && (77 <= celsius));
{
Console.WriteLine("\nTemperaturen är perfekt");
}
应该是:
do
{
// input stuff
} while (celsius < 73 || celsius > 77);
Console.WriteLine("\nTemperaturen är perfekt");
你看到difference了吗?
更好的是完全避免 do/while
构造。
while (celsius < 73 || celsius > 77)
{
// input stuff
}
Console.WriteLine("\nTemperaturen är perfekt");
您可能想用 (!(73 >= celsius && 77 <= celsius))
之类的内容替换 (celsius < 73 || celsius > 77)
但不要这样做。
尽可能避免否定逻辑,即使您认为否定逻辑更具 可读性,请相信我。那个小!
有时候真的很难看。即使是这样,人脑也需要更多的努力来思考 !(true)
甚至更多的努力来思考 !(false)
.
static double myconversion(string farenheitinput)
{
double temporary = 0;
try
{
double farenheit = double.Parse(farenheitinput);
temporary = farenheit;
}
catch
{
Console.Write("\nWrong! you must write a number");
}
double celsius = (temporary - 32) * 5 / 9;
return celsius;
}
public void Main(string[] args)
{
double celsius = 0;
do
{
Console.Write("\nWrite your temperature in farenheit: ");
string farenheitinput = (Console.ReadLine());
celsius = myconversion(farenheitinput);
if (celsius < 73)
{
Console.WriteLine("\nThat temperature is too cold");
}
else if (celsius > 77)
{
Console.WriteLine("\nThat temperature is too hot");
}
}
while ((celsius >= 73) && (celsius <= 77));
{
Console.WriteLine("\nThe temperature is perfect");
}
Console.Write("\nPress any key to close program...");
Console.ReadKey();
}
我正在使用一种方法将华氏度转换为摄氏度。 这是我用于我的循环和方法的代码,当我调试它时,如果用户输入 190 华氏度,它会显示双摄氏度 = 87.7,这对于 while 来说是一个错误条件,代码应该循环,但它不会t 并进入 while 代码并结束循环。我不知道是什么原因造成的,所以我想我是来这里问的。
循环条件表达式:
循环条件的工作方式是当它们评估为 true
.
运行 0 or more times
而 true
while (boolean expression|reference == true) { // do stuff }
运行 1 or more times
而 true
do { // do stuff } while (boolean expression|reference == true);
在任何一种情况下,第一个 false
遇到循环停止。
解决方案
你做了什么:
do
{
// input stuff
}
while ((73 >= celsius) && (77 <= celsius));
{
Console.WriteLine("\nTemperaturen är perfekt");
}
应该是:
do
{
// input stuff
} while (celsius < 73 || celsius > 77);
Console.WriteLine("\nTemperaturen är perfekt");
你看到difference了吗?
更好的是完全避免 do/while
构造。
while (celsius < 73 || celsius > 77)
{
// input stuff
}
Console.WriteLine("\nTemperaturen är perfekt");
您可能想用 (!(73 >= celsius && 77 <= celsius))
之类的内容替换 (celsius < 73 || celsius > 77)
但不要这样做。
尽可能避免否定逻辑,即使您认为否定逻辑更具 可读性,请相信我。那个小!
有时候真的很难看。即使是这样,人脑也需要更多的努力来思考 !(true)
甚至更多的努力来思考 !(false)
.