简体   繁体   中英

C# console.write showing statement

This is part of my code and I'm trying to make console.write to show new statement of "Please re-enter number greater than 20 : " only.

The problem is when I run my code it show "Please re-enter number greater than 20 : " as well as previous statement ("Enter amount of fuel used in litres : "). in one line.

How can I make it not to show first write statement ??

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

Move the line Console.Write("Enter amount of fuel used in litres : "); outside the while-loop. In your example it is inside the while loop, so it is repeated every time an incorrect answer is entered. If you move it in front of the loop, it will only be written once to the console.

...
bool badValue = true;
Console.Write("Enter amount of fuel used in litres : ");
do
{
    text = Console.ReadLine();
    ...

You can use Console.Clear() to clear the current contents of the Console. https://msdn.microsoft.com/en-us/library/system.console.clear(v=vs.110).aspx

For instance

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

I suggest changing the loop into infinite one; if question should be stated just once pull it out of the loop:

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

If I understood correctly, your problem is only an algorithmic problem :

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

By putting out the first write out of the loop, it only shows the first time and then does not come again. If you want it to completely disappear, just add a Console.Clear(); in the else statement to erase what was displayed before, and it's done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM