c# - Calculation is wrong -
what should happen
my exexcrise prgramm calculator able calculations in 'queue'.
example: user enters first number in txtzahl
, clicks 1 of button. needed calculation should saved in list numbers
, txtzahl
should cleared. user can enter new number , press button etc. when user clicks on btnequ
foreach should take every calculation list , calculation. if done result should displayed in txtzahl
.
problem
the calculations not correct. example 0.00 result 4-3.
i know idea class not best way, keep it, see teacher thinks it. thank helping!
code:
form1.cs
double ergebniss = 0; boolean firstrun = true; list<rechnung> numbers = new list<rechnung>(); rechnung.rechenart lastart; private void btnminus_click(object sender, eventargs e) { if (isvalid()) { if (firstrun) { ergebniss = convert.todouble(txtzahl.text); } numbers.add(new rechnung(convert.todouble(txtzahl.text), rechnung.rechenart.subtraktion)); lastart = rechnung.rechenart.subtraktion; clearandfocus(); } } private void btnequ_click(object sender, eventargs e) { foreach (rechnung r in numbers) { switch (r.getart()) { case rechnung.rechenart.subtraktion: { ergebniss -= r.getnumber(); break; } } } txtzahl.text = ergebniss.tostring("f2"); }
}
if (firstrun) { ergebniss = convert.todouble(txtzahl.text); firstrun = false; return; }
first forgot firstrun = false; after that
then advice display clean string
txtzahl.text = ergebniss.tostring();
you doesn't use lastart variable don't know if that's necessary.
Comments
Post a Comment