C#console.write显示语句

时间:2016-08-15 13:54:58

标签: c# if-statement console.writeline

这是我的代码的一部分,我试图让console.write显示&#34的新陈述;请重新输入大于20的数字:"仅

问题是当我运行我的代码时显示"请重新输入大于20的数字:"以及之前的陈述("输入用于升的燃料量:")。在一行。

如何让它不显示第一个写语句?

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

4 个答案:

答案 0 :(得分:5)

将行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();
    ...

答案 1 :(得分:0)

您可以使用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;
}

答案 2 :(得分:0)

我建议将循环更改为 infinite ;如果问题应该只是一旦将其拉出循环:

    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;            
    }

答案 3 :(得分:0)

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;
    }