Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wrote a quick code for factorizing formulas, however the line that takes creates the square root of D doesn’t work. The line is line 10. Any help is appreciated.

using System;
 public class MainClass {

 //Source Numbers
 public int A = 1;
 public int B = 3;
 public int C = 9;
 //Calculation Numbers
 public float Di;
 public static double Sqrt(double Di);   //This is the faulted line.
 //Answers
 public float X;
 public float X1;
 public float X2;

 public static void Main() {
Console.Writeline("D=", Di);
//Calculation for the Square root of D
 // (DSq)Math.Sqrt(Di);
   Di = B^2-4*A*C;
//Calculation for the answers
   if(Di>0) {
      X1 = ((0-B)-DSq)/(A*2);
      X2 = ((0-B)+DSq)/(A*2);
      Console.Writeline("X=", X1, " or X=", X2);
   }
   else if(Di=0) {
      X = 0-B;
      Console.Writeline("X=", X);
   }
   else {
   Console.Writeline("The formula cannot be solved.");
    }
   }
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
262 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are using a method definition with no body. In any case you dont need to invent the wheel, since Math has already a Math.Sqrt(), method. Try:

........
Di = B^2-4*A*C;
if (Di>0)
{
  var sqrDi =   Math.Sqrt(Di);
  .....
}
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...