C#中任意四个数字的总和

时间:2015-03-15 12:51:52

标签: c# rounding

总是一个是四个数小于一的数字的结果。

  1. 我将数字更改为十进制并浮动

  2. 我用数学,截断

  3. 我在计算中的每个数字之前写了两倍

  4. 总和的结果仍然是一个

    这是我的代码,我在代码中添加了注释来代替我的代码

    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    {
        public partial class WebForm3 : System.Web.UI.Page
        {
            public double[,] array = new double[4, 4];
            public double b1vsb1c1=1.0;
            public double b1vsb2c2;
            public double b1vsb3c3;
            public double b1vsb4c4;
    // //  .....here cotinue declear variable ,I deleteed to  shoritng the code;                  
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click1(object sender, EventArgs e)
            {
    
                string isChecked2 = RadioButtonList1.SelectedItem.Value.ToString();
    
                if (RadioButtonList1.SelectedItem.ToString() == "1")
                {
                    b1vsb1c1 = 1.0;
                    b2vsb1c1 = 1.0;
                }
    
                else if (RadioButtonList1.SelectedItem.ToString() == "3")
                {
                    b1vsb2c2 =3.0;
                    b2vsb1c1 =1.0/3.0;
                }
                
                //  .....here cotinue test radio button value ,I deleteed to  shoritng the code;                 
                    array[0,0]=b1vsb1c1;
                    array[0,1]=b1vsb2c2;
                    //....here cotinue assign value to array ,I deleteed to  shoritng the code;           
                    
               
                            for (int i = 0; i < 4; i++)
                            {
                                 sum = 0.0;
                                 av = 0.0;
                                for (int j = 0; j < 4; j++)
                                {
                                    sum=(double)sum + (double)array[i,j];
                                   
                                   
                                }
                                // here the sum always one why?!!
                                
                                av = (double)sum/(double)4.0;
                                Response.Write(av + "|   |");
                                // here is always result 0.25 why?
                            }
                            
    
                 }
        
        }
    }
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:2)

尝试使用Implicit typing:var来声明您的varibales 我试过你说的话;我有逻辑结果:这是一个示例exmaple:

 public class Program
    {
        public static void Main(string[] args)
        {
            var p1 = 0.120336943441637;
            var p5 = 0.01323706377858;
            var p9 = 0.120336943441637;
            var p13 = 0.120336943441637;
            var sum = p1 + p5 + p9 + p13;
            Console.WriteLine(sum);
        }

    }

结果= 0.374247894103491

Code Run