简体   繁体   中英

Calculating Sum of Squares in C#

I have the following code to calculate the sum of squares. The logic looks good but it's not the output I'm looking for.

I enter an int value of 3 and get the following response:

The sum of squares for 1 is 1

The sum of squares for 2 is 5

The sum of squares for 3 is 14

What I want to show in the output is only the value I entered. "The sum of squares for 3 is 14"

What am I doing wrong here?

using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;

    namespace Lab4
   {
    class Program
    {
        static void Main(string[] args)
        {
            Statistics();
        }

        static void Statistics()
        {

            SumofSquares();

            Console.ReadLine();
        }
        static void SumofSquares()
        {
            int num;
            Console.Write("Enter an int: ");
            string input = Console.ReadLine();

            int.TryParse(input, out num);
            Squares(num);
        }

        static long Squares(long value)
        {
            long totalSum;

            if (value > 0)
            {
                totalSum = Squares(value - 1) + (value*value);

            }
            else if (value == 1)
            {
                totalSum = 1;
            }
            else
            {
                totalSum = 0;
            }


            return totalSum; 

        }
    }

The code you posted doesn't print The sum of squares for X is Y at all, but from the behavior you described, I assume you once had a print statement in the Squares() method. Since this method is recursive , such a print statement would execute for each nested call.

Instead, put the print statement after the final result is obtained when the outermost Squares() invocation returns. Ie, in SumofSquares() :

static void SumofSquares()
{
    int num;
    Console.Write("Enter an int: ");
    string input = Console.ReadLine();

    int.TryParse(input, out num);
    long result = Squares(num);
    Console.WriteLine("The sum of squares for {0} is {1}", num, result);
}

You'd be well served spending some time learning how to use the debugger in Visual Studio . To find out why a print statement is executing when you think it shouldn't, set a breakpoint on the statement. When the breakpoint is triggered, you can use the Locals window to see the current values of all variables in scope, and you can use the Call Stack window to see what called the current method (and what called that, and so on).

Here's a screenshot. The Call Stack shows the recursion in Squares() . From here you could use the Step Over command to advance the program one line of code at a time to see exactly what's going on.

调试器示例

Some other tips (unrelated to the problem you posted):

  • You should have error checking on int.TryParse() .
  • Your else if (value == 1) condition in Squares() is useless: its body will never execute because it comes after a broader condition, if (value > 0) .

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