Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

c# - Square root line doesn’t work

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

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

1 Reply

0 votes
by (71.8m points)

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);
  .....
}
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...