2017-03-05 10 views
-1

Ich habe Probleme mit addind float in meinem Rechner. Wer kann mir helfen, diesen Code zu ändern, bitte. Ich habe keine Ahnung, was ich falsch gemacht habe.C# - Rechner - berechnen mit floats

public float CalculateFloatResult(string operation, float num1, float num2) 
    { 
     float resut = 0; 
     try 
     { 
      if (txtResult.Text.Contains("+") || txtResult.Text.Contains("-") || txtResult.Text.Contains("*") || txtResult.Text.Contains("/") || txtResult.Text.Contains("%")) 
      { 
       switch (operation) 
       { 
        case "plus": 
         resut = num1 + num2; 
         break; 
        case "minus": 
         resut = num1 - num2; 
         break; 
        case "multiply": 
         resut = num1 * num2; 
         break; 
        case "divide": 
         resut = num1/num2; 
         break; 
        case "modulus": 
         if (num2 != 0.0) 
          resut = num1 % num2; 
         else 
          MessageBox.Show("Cannot find Modulus of Zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
         break; 
        default: 
         break; 
       } 
      } 

      return resut; 
     } 
     catch 
     { 
      return resut; 
     } 
    } 
+0

Und das Problem ist ...? – Guy

+0

und was genau ist das Problem? Falsche Summe? – Whencesoever

+0

ich kann nicht 2 Zahlen hinzufügen :( –

Antwort

0

versuchen, Float zu ändern result = 0; float result = 0.00; Hoffe, dies wird das Problem für Sie lösen 0

+0

leider keine :( –

+0

PLZ erklären Ihr Problem mit ein bisschen mehr Erklärung –

0

Wenn Sie Probleme mit Punkt haben, sollten Sie Float auf Double ändern. oder konvertieren, wenn Sie Metode rufen möchten diese

+0

float myFloat = Berechnen ("plus", float.Parse (split [0]), float.Parse (split [1])); –

+0

es ist nicht gut :( –

+0

sollten Sie Float zu Double ändern, ich teste seine Arbeit. –