计算在C#中的代数表达式 [英] Calculate the algebraic expression in C#

查看:222
本文介绍了计算在C#中的代数表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算代数表达式Z,对于其中n是由用户输入的。使用2 for循环来解决问题。

Calculate the algebraic expression Z, for which n is inputted by user. Use 2 for loops to solve the problem.

我到目前为止的代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            int n, k = 1;
            double z;
            float sum, p;
            n = Console.Read();

            for (int i = 0; i < n; i+=2)
            {
                sum += p;

                for (int j = 0; j < n; j++)
                {
                    p *= (3 * k + 2);
                }

            }

        }
    }
}

我完全堆栈中的for循环...任何帮助表示赞赏。

I'm totally stack in the for loops... any help is appreciated.

推荐答案


  • p的被初始化为1,0 * X == 0

  • 此外i和j(为什么不是K)必须被初始化为1,因为它是由您的公式

  • 您已经在那里计算出产品后要总结的产品,你会增加1到正确的结果,否则,你会不会增加,最后计算出产品所需

所以,下面的代码应该给予正确的结果:

So the code below should give the correct result:

        float sum = 0;

        int n = Console.Read();

        for (int i = 1; i <= n; i++)
        {

          float p = 1;
          for (int k = 1; k <= i+2; k++)
          {
            p *= (3 * k + 2);
          }              

          sum += p;
        }

这篇关于计算在C#中的代数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆