c# – 如何将null变为0

前端之家收集整理的这篇文章主要介绍了c# – 如何将null变为0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我建立了一个小程序来计算15个或更少的平均数.有15个文本框,每个文本框的默认值为“0”.程序知道获取所有类型数字的总和,并将其除以不返回“0”的文本框的数量.但是如果用户错误删除了其中一个文本框中的’0’之一..运行时错误.

最初我通过写这个“if语句”15次(每个文本框一个)来解决这个问题:

  1. if (t1.Text == "") { tr1 = 0; }
  2. else
  3. {
  4. tr1 = Double.Parse(t1.Text);
  5. }

代码检查文本框中是否有东西(例如,名为t1),如果为true,则程序给出双’tr1′(不要与’t1’混淆),值为’0’如果为false,则代码将’t1’的文本赋予’t1′.

我不得不写’如果’15次.我想知道我是否可以用数组和for循环编写相同的代码,以及如何?

这是整个代码(抱歉var名称与var的使用不相似.):

  1. private void goyouidiot_Click(object sender,EventArgs e)
  2. {
  3. double tr1;
  4. double tr2;
  5. double tr3;
  6. double tr4;
  7. double tr5;
  8. double tr6;
  9. double tr7;
  10. double tr8;
  11. double tr9;
  12. double tr10;
  13. double tr11;
  14. double tr12;
  15. double tr13;
  16. double tr14;
  17. double tr15;
  18. if (t1.Text == "") { tr1 = 0; }
  19. else
  20. {
  21. tr1 = Double.Parse(t1.Text);
  22. }
  23. if (t2.Text == "") { tr2 = 0; }
  24. else
  25. {
  26. tr2 = Double.Parse(t2.Text);
  27. }
  28.  
  29. if (t3.Text == "") { tr3 = 0; }
  30. else
  31. {
  32. tr3 = Double.Parse(t3.Text);
  33. }
  34.  
  35.  
  36. if (t4.Text == "") { tr4 = 0; }
  37. else
  38. {
  39. tr4 = Double.Parse(t4.Text);
  40. }
  41.  
  42.  
  43. if (t5.Text == "") { tr5 = 0; }
  44. else
  45. {
  46. tr5 = Double.Parse(t5.Text);
  47. }
  48.  
  49. if (t6.Text == "") { tr6 = 0; }
  50. else
  51. {
  52. tr6 = Double.Parse(t6.Text);
  53. }
  54.  
  55.  
  56. if (t7.Text == "") { tr7 = 0; }
  57. else
  58. {
  59. tr7 = Double.Parse(t7.Text);
  60. }
  61.  
  62.  
  63. if (t8.Text == "") { tr8 = 0; }
  64. else
  65. {
  66. tr8 = Double.Parse(t8.Text);
  67. }
  68.  
  69. if (t9.Text == "") { tr9 = 0; }
  70. else
  71. {
  72. tr9 = Double.Parse(t9.Text);
  73. }
  74.  
  75.  
  76. if (t10.Text == "") { tr10 = 0; }
  77. else
  78. {
  79. tr10 = Double.Parse(t10.Text);
  80. }
  81.  
  82.  
  83. if (t11.Text == "") { tr11 = 0; }
  84. else
  85. {
  86. tr11 = Double.Parse(t11.Text);
  87. }
  88.  
  89.  
  90. if (t12.Text == "") { tr12 = 0; }
  91. else
  92. {
  93. tr12 = Double.Parse(t12.Text);
  94. }
  95.  
  96. if (t13.Text == "") { tr13 = 0; }
  97. else
  98. {
  99. tr13 = Double.Parse(t13.Text);
  100. }
  101.  
  102.  
  103. if (t14.Text == "") { tr14 = 0; }
  104. else
  105. {
  106. tr14 = Double.Parse(t14.Text);
  107. }
  108.  
  109.  
  110. if (t15.Text == "") { tr15 = 0; }
  111. else
  112. {
  113. tr15 = Double.Parse(t15.Text);
  114. }
  115. double[] sch = { tr1,tr2,tr3,tr4,tr5,tr6,tr7,tr8,tr9,tr10,tr11,tr12,tr13,tr14,tr15 };
  116. double total = 0;
  117. double sorf = 0;
  118. for (int i = 0; i != 14; i++)
  119. {
  120.  
  121. sorf = sorf + sch[i];
  122.  
  123. if (sch[i] > 0)
  124. { total++; }
  125.  
  126. }
  127.  
  128. double totalic = sorf / total;
  129. string glass = totalic.ToString();
  130. result.Text = ("your score: " + glass);
  131. }

解决方法

  1. Double.TryParse(t1.Text.Trim(),out tr1);

将tr1设置为文本框的数值,如果由于某种原因无法将其转换为0.0.如果转换成功,它也将返回true,如果失败则返回false,但如果默认值为0.0,则不关心返回值.

额外奖励:如果有人决定将“这不是数字”,则不会抛出异常.进入文本框.它只会看到值为0.

要在数组中执行此操作…

  1. TextBox t[] = { t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15 };
  2. double tr[] = new double[t.Length];
  3.  
  4. for (int i = 0; i < t.Length; ++i)
  5. {
  6. Double.TryParse(t[i].Text.Trim(),out tr[i]);
  7. }

更新:

注意,期望能够计算包含0的数字的平均值是完全合理的.为此:

  1. TextBox t[] = { t1,t15 };
  2. double tr[] = new double[t.Length];
  3. int valid_count = 0;
  4.  
  5. for (int i = 0; i < t.Length; ++i)
  6. {
  7. if (Double.TryParse(t[i].Text.Trim(),out tr[i])) ++valid_count;
  8. }

将TextBoxes的默认值设置为空白(“”),然后您将知道用户输入的合法数量为0,以及有多少空白.将总和除以valid_count以获得准确的平均值. (但请确保valid_count> 0,否则您可能会获得除零异常.)

猜你在找的C#相关文章