经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
unity实现简单计算器
来源:jb51  时间:2021/8/9 10:07:04  对本文有异议

本文实例为大家分享了unity实现简单计算器的具体代码,供大家参考,具体内容如下

  1. using System.Text;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using DG.Tweening;
  5. using System;
  6.  
  7. public class Calculator : MonoBehaviour
  8. {
  9. public Text SpendText;
  10. private StringBuilder spendPrice;//初始金额
  11. private string rmbSymbol;
  12. private float totalPrice, spendPrices;//总和,初始金额
  13. private bool isFirstDecrease;//避免减为零后的第二次起不能为负
  14. private bool? isPlusOrDecrease, countType;//点击加减符号,点击等号
  15. public Button PointButton;
  16. private int count;//限制最大输入数
  17. private void Start()
  18. {
  19. spendPrice = new StringBuilder();
  20. totalPrice = 0;
  21. spendPrices = 0;
  22. rmbSymbol = "<size='50'>¥</size> ";
  23. isPlusOrDecrease = null;//true为加,false为减
  24. countType = null;//true为加,false为减
  25. isFirstDecrease = true;
  26. count = 0;
  27. }
  28.  
  29. public void PointButtonController(bool type)
  30. {
  31. PointButton.interactable = type;
  32. }
  33.  
  34. public void InputNumber(int num)
  35. {
  36. //按钮
  37. switch (num)
  38. {
  39. case 0:
  40. if (count < 11)
  41. {
  42. count++;
  43. spendPrice.Append("0");
  44. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  45. }
  46. break;
  47. case 1:
  48. if (count < 11)
  49. {
  50. count++;
  51. spendPrice.Append("1");
  52. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  53. }
  54. break;
  55. case 2:
  56. if (count < 11)
  57. {
  58. count++;
  59. spendPrice.Append("2");
  60. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  61. }
  62. break;
  63. case 3:
  64. if (count < 11)
  65. {
  66. count++;
  67. spendPrice.Append("3");
  68. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  69. }
  70. break;
  71. case 4:
  72. if (count < 11)
  73. {
  74. count++;
  75. spendPrice.Append("4");
  76. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  77. }
  78. break;
  79. case 5:
  80. if (count < 11)
  81. {
  82. count++;
  83. spendPrice.Append("5");
  84. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  85. }
  86. break;
  87. case 6:
  88. if (count < 11)
  89. {
  90. count++;
  91. spendPrice.Append("6");
  92. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  93. }
  94. break;
  95. case 7:
  96. if (count < 11)
  97. {
  98. count++;
  99. spendPrice.Append("7");
  100. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  101. }
  102. break;
  103. case 8:
  104. if (count < 11)
  105. {
  106. count++;
  107. spendPrice.Append("8");
  108. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  109. }
  110. break;
  111. case 9:
  112. if (count < 11)
  113. {
  114. count++;
  115. spendPrice.Append("9");
  116. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  117. }
  118. break;
  119. case 10:
  120. if (count < 11)
  121. {
  122. count += 2;
  123. spendPrice.Append("00");
  124. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  125. }
  126. break;
  127. case 11://加
  128. isPlusOrDecrease = true;
  129. countType = true;
  130. count = 0;
  131. if (!spendPrice.ToString().Equals(""))
  132. {
  133. if ((totalPrice + float.Parse(spendPrice.ToString()) < 99999999999) && !totalPrice.ToString().Contains("E"))
  134. {
  135. PointButtonController(true);
  136. if (totalPrice != 0)//避免第一次点击加号时没反应
  137. {
  138. TotalCount();
  139. }
  140. CountNum();
  141. }
  142. else
  143. {
  144. count = 0;
  145. PointButtonController(true);
  146. totalPrice = 0;
  147. spendPrice.Clear();
  148. SpendText.DOText(rmbSymbol, 0);
  149. isFirstDecrease = true;
  150. }
  151. }
  152. break;
  153. case 12://减
  154. isPlusOrDecrease = false;
  155. countType = false;
  156. count = 0;
  157. if (!spendPrice.ToString().Equals(""))
  158. {
  159. if ((totalPrice + float.Parse(spendPrice.ToString()) < 99999999999) && !totalPrice.ToString().Contains("E"))
  160. {
  161. PointButtonController(true);
  162. if (totalPrice != 0)//避免第一次点击减号时没反应
  163. {
  164. TotalCount();
  165. }
  166. CountNum();
  167. }
  168. else
  169. {
  170. count = 0;
  171. PointButtonController(true);
  172. totalPrice = 0;
  173. spendPrice.Clear();
  174. SpendText.DOText(rmbSymbol, 0);
  175. isFirstDecrease = true;
  176. }
  177. }
  178. break;
  179. case 13://点
  180. PointButtonController(false);
  181. spendPrice.Append(".");
  182. SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
  183. break;
  184. case 14://等号
  185. count = 0;
  186. if (!spendPrice.ToString().Equals(""))
  187. {
  188. if ((totalPrice + float.Parse(spendPrice.ToString()) < 9999999999) && !totalPrice.ToString().Contains("E"))
  189. {
  190. PointButtonController(true);
  191. TotalCount();
  192. }
  193. else
  194. {
  195. count = 0;
  196. PointButtonController(true);
  197. totalPrice = 0;
  198. spendPrice.Clear();
  199. SpendText.DOText(rmbSymbol, 0);
  200. isFirstDecrease = true;
  201. }
  202. }
  203. break;
  204. case 15://清零
  205. count = 0;
  206. PointButtonController(true);
  207. totalPrice = 0;
  208. spendPrice.Clear();
  209. SpendText.DOText(rmbSymbol, 0);
  210. isFirstDecrease = true;
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. public void CountNum()
  217. {
  218. if (spendPrice.ToString().StartsWith("0") || spendPrice.ToString().Equals(""))//去除开始的无效零
  219. {
  220. if (spendPrice.ToString().TrimStart('0') == "" || spendPrice.ToString().TrimStart('0').TrimEnd('0') == ".")//0000,00.00,0.,.0
  221. {
  222. spendPrices = 0;
  223. }
  224. else
  225. {
  226. spendPrices = float.Parse((float.Parse(spendPrice.ToString().TrimStart('0'))).ToString("f2"));
  227. }
  228. }
  229. else
  230. {
  231. spendPrices = float.Parse((float.Parse(spendPrice.ToString())).ToString("f2"));
  232. }
  233. if (isPlusOrDecrease == true && totalPrice != 0 && spendPrices != 0)
  234. {
  235. totalPrice += spendPrices;
  236. spendPrice.Clear();
  237. SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
  238. isPlusOrDecrease = null;
  239. }
  240. else if (isPlusOrDecrease == true && totalPrice == 0 && spendPrices != 0 && spendPrices != 0)
  241. {
  242. totalPrice = spendPrices;
  243. spendPrice.Clear();
  244. }
  245. if (isPlusOrDecrease == false && totalPrice == 0 && spendPrices != 0 && isFirstDecrease)
  246. {
  247. totalPrice = spendPrices;
  248. spendPrice.Clear();
  249. isFirstDecrease = false;
  250. }
  251. else if (isPlusOrDecrease == false && spendPrices != 0)
  252. {
  253. totalPrice -= spendPrices;
  254. spendPrice.Clear();
  255. SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
  256. isPlusOrDecrease = null;
  257. }
  258. }
  259.  
  260. public void TotalCount()
  261. {
  262. if (spendPrice.ToString().StartsWith("0") || spendPrice.ToString().Equals(""))
  263. {
  264. if (spendPrice.ToString().TrimStart('0') == "" || spendPrice.ToString().TrimStart('0').TrimEnd('0') == ".")
  265. {
  266. spendPrices = 0;
  267. }
  268. else
  269. {
  270. spendPrices = float.Parse((float.Parse(spendPrice.ToString().TrimStart('0'))).ToString("f2"));
  271. }
  272. }
  273. else
  274. {
  275. spendPrices = float.Parse((float.Parse(spendPrice.ToString())).ToString("f2"));
  276. }
  277. if (spendPrices != 0)
  278. {
  279. if (countType == true)
  280. {
  281. totalPrice += spendPrices;
  282. }
  283. else if (countType == false)
  284. {
  285. totalPrice -= spendPrices;
  286. }
  287. spendPrice.Clear();
  288. SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
  289. countType = null;
  290. }
  291. }
  292. //将科学计数法转化为普通数字
  293. private Decimal ChangeDataToD(string strData)
  294. {
  295. Decimal dData = 0.0M;
  296. if (strData.Contains("E"))
  297. {
  298. dData = Decimal.Parse(strData, System.Globalization.NumberStyles.Float);
  299. }
  300. return dData;
  301. }
  302. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号