【发布时间】:2019-12-09 14:00:24
【问题描述】:
我有一个 WPF 和 C# 和 SQLITE 系统(这里不重要),它计算人们一天喝的水量。用户有权更改 ML 中的杯子尺寸值和当天的元数据(在这种情况下并不重要)。
事物的计算是:
takewatervalue = int.Parse(this.takeWater.Text) + takewatervalue;
counter = takewatervalue * cupml;
textCounter.Text = counter.ToString();
当人们没有在另一个窗口中更改 cupml 的值(如下所示)时,此计算可以正常工作,当他们更改时,会发生这种情况:
改变cupml值之前的例子:
cupml = 200 // actual value of cupml choosen for the user
takewatervalue = int.Parse(this.takeWater.Text)(user input = 1) + takewatervalue(0); //
counter = takewatervalue //1 * cupml //200; // = 200 ml -- this is right no problem here
textCounter.Text = counter.ToString(); // showing to the user the counter and that's good
又一轮:
cupml = 200 // actual value of cupml choosen for the user
takewatervalue = int.Parse(this.takeWater.Text)(user input = 1) + takewatervalue(1); //
counter = takewatervalue //2 * cupml //200; // = 400 ml -- this is right no problem here
textCounter.Text = counter.ToString(); // showing to the user the counter and that's good
但是当我们更改cupml时,会发生这种情况:
actual conter value = 400 ML
cupml = 300 // actual value of cupml choosen for the user in another window
takewatervalue = int.Parse(this.takeWater.Text)(user input = 1) + takewatervalue(2); //
counter = takewatervalue //3 * cupml //300; // = 600 ml -- this is not right, the right value is 600 + 400(the old counter) = 1000 ML
textCounter.Text = counter.ToString(); // showing the wrong counter;
有人知道用另一种方式计算吗?
完整的参考代码在这里有一个 SQLITE 和其他东西的数据库,但这里的重点是这个计算:https://github.com/lucasbarbosagit/BebaAguaAPP
【问题讨论】:
-
我很困惑。
takewatervalue是什么?counter应该算什么? -
哦,请不要让我们下载整个项目。发布minimal reproducible example。
-
我会尽量让这更清楚。
-
我将从重构实际计算开始。原则上它必须类似于
newSum = oldSum + (cups * size)所以让它成为一个函数。为它编写单元测试。如果整个事情仍然不起作用,那么您会知道公式是正确的,但是您输入了错误的内容。 -
不应该 counter = counter + takewatervalue * cupml?
标签: c# wpf visual-studio sqlite windows-10