C# console.write 显示语句
C# console.write showing statement
这是我的代码的一部分,我正在尝试让 console.write 仅显示 "Please re-enter number greater than 20 : " 的新语句。
问题是当我 运行 我的代码显示 "Please re-enter number greater than 20 : " 以及之前的语句 ("Enter amount of fuel used in litres : ")。在一行中。
如何让它不显示第一个写入语句??
static double InputFuel()
{
double fFuel;
string text;
bool badValue = true;
do
{
Console.Write("Enter amount of fuel used in litres : ");
text = Console.ReadLine();
if (double.TryParse(text, out fFuel) && fFuel >= 20)
{
badValue = false;
}
else
{
Console.WriteLine("\n\t {0} is below the minimum value of 20 \n\n", text);
Console.Write("Please re-enter number greater than 20 : ");
}
} while (badValue);
return fFuel;
}//end InputTemp
您可以使用 Console.Clear() 清除控制台的当前内容。 https://msdn.microsoft.com/en-us/library/system.console.clear(v=vs.110).aspx
例如
static double InputFuel()
{
double fFuel;
string text;
bool badValue = true;
do
{
Console.Write("Enter amount of fuel used in litres : ");
text = Console.ReadLine();
if (double.TryParse(text, out fFuel) && fFuel >= 20)
{
badValue = false;
}
else
{
Console.Clear();
Console.WriteLine("\n\t {0} is below the minimum value of 20 \n\n", text);
Console.Write("Please re-enter number greater than 20 : ");
}
} while (badValue);
return fFuel;
}
将行 Console.Write("Enter amount of fuel used in litres : ");
移到 while 循环之外。在您的示例中,它位于 while 循环内,因此每次输入不正确的答案时都会重复。如果你把它移到循环前面,它只会被写入控制台一次。
...
bool badValue = true;
Console.Write("Enter amount of fuel used in litres : ");
do
{
text = Console.ReadLine();
...
我建议将循环更改为无限一个;如果问题应该只 陈述一次 将其从循环中拉出来:
Console.Write("Enter amount of fuel used in litres : ");
while (true) {
text = Console.ReadLine();
if (!double.TryParse(text, out fFuel))
Console.Write("Please re-enter number in correct format: ");
else if (fFuel <= 0)
Console.Write("Please re-enter number greater than 0 : ");
else if (fFuel >= 20)
Console.Write("Please re-enter number less than 20 : ");
else
break;
}
static double InputFuel()
{
double fFuel;
string text;
bool badValue = true;
bool notFirst = false;
do
{
if(!notFirst)
Console.Write("Enter amount of fuel used in litres : ");
notFirst = true;
text = Console.ReadLine();
if (double.TryParse(text, out fFuel) && fFuel >= 20)
{
badValue = false;
}
else
{
Console.WriteLine("\n\t {0} is below the minimum value of 20 \n\n", text);
Console.Write("Please re-enter number greater than 20 : ");
}
} while (badValue);
return fFuel;
}
如果我没理解错的话,你的问题只是一个算法问题:
static double InputFuel()
{
double fFuel;
string text;
bool badValue = true;
Console.Write("Enter amount of fuel used in litres : ");
do
{
text = Console.ReadLine();
if (double.TryParse(text, out fFuel) && fFuel >= 20)
{
badValue = false;
}
else
{
Console.Clear();
Console.WriteLine("\n\t {0} is below the minimum value of 20 \n\n", text);
Console.Write("Please re-enter number greater than 20 : ");
}
} while (badValue);
return fFuel;
}//end InputTemp
把第一次写出循环,只显示第一次,以后不会再来了。如果你想让它完全消失,只要在else语句中加一个Console.Clear();
,把之前显示的东西擦掉,就搞定了。
这是我的代码的一部分,我正在尝试让 console.write 仅显示 "Please re-enter number greater than 20 : " 的新语句。
问题是当我 运行 我的代码显示 "Please re-enter number greater than 20 : " 以及之前的语句 ("Enter amount of fuel used in litres : ")。在一行中。
如何让它不显示第一个写入语句??
static double InputFuel()
{
double fFuel;
string text;
bool badValue = true;
do
{
Console.Write("Enter amount of fuel used in litres : ");
text = Console.ReadLine();
if (double.TryParse(text, out fFuel) && fFuel >= 20)
{
badValue = false;
}
else
{
Console.WriteLine("\n\t {0} is below the minimum value of 20 \n\n", text);
Console.Write("Please re-enter number greater than 20 : ");
}
} while (badValue);
return fFuel;
}//end InputTemp
您可以使用 Console.Clear() 清除控制台的当前内容。 https://msdn.microsoft.com/en-us/library/system.console.clear(v=vs.110).aspx
例如
static double InputFuel()
{
double fFuel;
string text;
bool badValue = true;
do
{
Console.Write("Enter amount of fuel used in litres : ");
text = Console.ReadLine();
if (double.TryParse(text, out fFuel) && fFuel >= 20)
{
badValue = false;
}
else
{
Console.Clear();
Console.WriteLine("\n\t {0} is below the minimum value of 20 \n\n", text);
Console.Write("Please re-enter number greater than 20 : ");
}
} while (badValue);
return fFuel;
}
将行 Console.Write("Enter amount of fuel used in litres : ");
移到 while 循环之外。在您的示例中,它位于 while 循环内,因此每次输入不正确的答案时都会重复。如果你把它移到循环前面,它只会被写入控制台一次。
...
bool badValue = true;
Console.Write("Enter amount of fuel used in litres : ");
do
{
text = Console.ReadLine();
...
我建议将循环更改为无限一个;如果问题应该只 陈述一次 将其从循环中拉出来:
Console.Write("Enter amount of fuel used in litres : ");
while (true) {
text = Console.ReadLine();
if (!double.TryParse(text, out fFuel))
Console.Write("Please re-enter number in correct format: ");
else if (fFuel <= 0)
Console.Write("Please re-enter number greater than 0 : ");
else if (fFuel >= 20)
Console.Write("Please re-enter number less than 20 : ");
else
break;
}
static double InputFuel()
{
double fFuel;
string text;
bool badValue = true;
bool notFirst = false;
do
{
if(!notFirst)
Console.Write("Enter amount of fuel used in litres : ");
notFirst = true;
text = Console.ReadLine();
if (double.TryParse(text, out fFuel) && fFuel >= 20)
{
badValue = false;
}
else
{
Console.WriteLine("\n\t {0} is below the minimum value of 20 \n\n", text);
Console.Write("Please re-enter number greater than 20 : ");
}
} while (badValue);
return fFuel;
}
如果我没理解错的话,你的问题只是一个算法问题:
static double InputFuel()
{
double fFuel;
string text;
bool badValue = true;
Console.Write("Enter amount of fuel used in litres : ");
do
{
text = Console.ReadLine();
if (double.TryParse(text, out fFuel) && fFuel >= 20)
{
badValue = false;
}
else
{
Console.Clear();
Console.WriteLine("\n\t {0} is below the minimum value of 20 \n\n", text);
Console.Write("Please re-enter number greater than 20 : ");
}
} while (badValue);
return fFuel;
}//end InputTemp
把第一次写出循环,只显示第一次,以后不会再来了。如果你想让它完全消失,只要在else语句中加一个Console.Clear();
,把之前显示的东西擦掉,就搞定了。