简体   繁体   中英

Adding list of integers on C#

I'm doing an exercise that requires I have someone enter a list of numbers, then when they enter "ok" the program adds the list of numbers they entered. Right now my program is returning 0 no matter how many numbers are added. I suspect the problem is with the last 3 lines of code, but can't figure out what I'm doing wrong. Sorry about how inefficient the code is. This is only my third day learning so I'm trying to format it in a way that makes sense to me, I know there are much more efficient ways of doing this though.

static void Main(string[] args)
{
    bool isOk = new bool();          
    bool isNumber = new bool();

    var listOfNumbers = new List<string>();
    var text = "0";

    int ignoreMe = new int();
    int sumOfNumbers = new int();
    int numberNum = new int();
    var listOfNumbersNum = new List<int>();

    while (!isOk)
    {
        Console.WriteLine("Enter a number, or ok to finish");
        text = Console.ReadLine();
        bool IsNumber = Int32.TryParse(text, out ignoreMe);
        if (isNumber)
        {
            numberNum = Int32.Parse(text);
            listOfNumbersNum.Add(numberNum);
        }
        else
        {
            if (text.Equals("ok", StringComparison.OrdinalIgnoreCase))
            {
                sumOfNumbers = listOfNumbersNum.Sum();
                Console.WriteLine(sumOfNumbers);
                isOk = true;
            }
        }
    }
}

Your problem is here:

bool IsNumber = Int32.TryParse(text, out ignoreMe);

if (isNumber)
{
    // rest of code omitted

You are creating a new variable named IsNumber to capture the return value of int.TryParse , but you're checking the value of your original variable, isNumber in your if condition. Instead, you should just assign the result to the original variable:

isNumber = Int32.TryParse(text, out ignoreMe);

if (isNumber)
{
    // rest of code omitted

Note that you really don't need to declare a variable to capture this result at all, since you're only using it once. You can put the TryParse call inside the if condition:

if (Int32.TryParse(text, out ignoreMe))
{
    listOfNumbersNum.Add(ignoreMe);
}
// Console.ReadLine() will never return null, so you can remove that check
else if (text.Equals("ok", StringComparison.OrdinalIgnoreCase))
{
    // And since you only use sum once, you don't need to capture it in a variable
    Console.WriteLine("Result of sum: " + listOfNumbersNum.Sum());
    isOk = true;
}

The problem is that you parse value into ignoreMe. Then you adds numberNum into list. Initial value of numberNum is 0 and it has never been changed. This code should work for you :

static void Main(string[] args)
{
    bool isOk = new bool();
    var listOfNumbers = new List<string>();
    string text;

    int numberNum;
    var listOfNumbersNum = new List<int>();

    while (!isOk)
    {
        Console.WriteLine("Enter a number, or ok to finish");
        text = Console.ReadLine();
        bool isNumber = Int32.TryParse(text, out numberNum);
        if (isNumber)
        {
            listOfNumbersNum.Add(numberNum);
        }
        else
        {
            if (text.Equals("ok", StringComparison.OrdinalIgnoreCase))
            {
                int sumOfNumbers = listOfNumbersNum.Sum();
                Console.WriteLine(sumOfNumbers);
                isOk = true;
            }

        }

    }
}

Firstly mistake in

bool IsNumber = Int32.TryParse(text, out ignoreMe);
if (isNumber)

IsNumber and isNumber is not same thing

Same code with a little bit changes

static void Main(string[] args)
{
    var isOk = false;
    var listOfNumbersNum = new List<int>();

    while (!isOk)
    {
        Console.WriteLine("Enter a number, or ok to finish");
        var text = Console.ReadLine();
        var isNumber = int.TryParse(text, out var numberNum);
        if (isNumber)
        {
            listOfNumbersNum.Add(numberNum);
        }
        else
        {
            if (text != null && text.Equals("ok", StringComparison.OrdinalIgnoreCase))
            {
                var sumOfNumbers = listOfNumbersNum.Sum();
                Console.WriteLine("Result of sum: " + sumOfNumbers);
                isOk = true;
            }
        }
    }

    Console.WriteLine("Press any key to exit");
    Console.ReadLine();
}

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