简体   繁体   中英

C# do while loops - How to remember user input?

I'm new to C#, and i'm writing a do while loop that continues to ask the user to enter "price", until they enter "-1" for price.

Afterwards, I need to add up all the values for price they entered and declare that as the subtotal.

The problem I have is that it only remember the last number entered, which would be -1. What would I have to do to fix this?

using System;

namespace ConsoleApp1
{
class Program
{
    static void Main()
    {
        Console.WriteLine("Your Receipt");
        Console.WriteLine("");
        Console.WriteLine("");

        decimal count;
        decimal price;
        decimal subtotal;
        decimal tax;
        decimal total;

        count = 1;

        do
        {
            Console.Write("Item {0} Enter Price: ", count);
            ++count;
            price = Convert.ToDecimal(Console.ReadLine());


        } while (price != -1);

        subtotal = Convert.ToInt32(price);
        Console.Write("Subtotal: ${0}", subtotal);

    }
}

}

Try this variation to Artem's answer. I think this is a little cleaner.

int count = 0;
decimal input = 0;
decimal price = 0;

while (true)
{
    Console.Write("Item {0} Enter Price: ", count++);
    input = Convert.ToDecimal(Console.ReadLine());
    if (input == -1)
    {
        break;
    }
    price += input;
}

Use a list and keep adding the entries to the list. Or you can keep a running total in another integer.

Something like:

int total = 0; // declare this before your loop / logic other wise it will keep getting reset to 0.
total = total+ input;

In each iteration of the loop, you overwrite the value of price . Separate input and storage price .

decimal input = 0;

do
{
    Console.Write("Item {0} Enter Price: ", count);
    ++count;
    input = Convert.ToDecimal(Console.ReadLine());
    if (input != -1)
        price += input;
} while (input != -1);

Please try to use this

using System;

namespace ConsoleApp1
{
class Program
{
    static void Main()
    {
        Console.WriteLine("Your Receipt");
        Console.WriteLine("");
        Console.WriteLine("");

        decimal count;
        decimal price;
        decimal subtotal = 0m; //subtotal is needed to be initialized from 0
        decimal tax;
        decimal total;

        count = 1;

        do
        {
            Console.Write("Item {0} Enter Price: ", count);
            ++count;
            price = Convert.ToDecimal(Console.ReadLine());
            if (price != -1)  //if the console input -1 then we dont want to make addition
              subtotal += price; 

        } while (price != -1);

        //subtotal = Convert.ToInt32(price); this line is needed to be deleted. Sorry I didnt see that.
        Console.Write("Subtotal: ${0}", subtotal); //now subtotal will print running total

    }
}
}

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