Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 曼德尔数公式;字符串不包含接受0个参数的构造函数";_C# - Fatal编程技术网

C# 曼德尔数公式;字符串不包含接受0个参数的构造函数";

C# 曼德尔数公式;字符串不包含接受0个参数的构造函数";,c#,C#,(我对编程和本网站都是初学者。提前:如果我以错误的方式使用本网站,我会道歉。) (使用C#) 我正试图编写一种计算曼德尔数的方法(目的是在另一个程序中使用该方法),但我收到一条错误消息,上面说: “错误1‘字符串’不包含接受0个参数的构造函数” 在此之前,“并非所有代码路径都返回值”,但我想我用convert.tostring修复了这一问题。然而,这个错误对我来说是新的 我不知道这意味着什么,谁能解释一下问题是什么 使用制度 命名空间Windows窗体应用程序1{ public class fo

(我对编程和本网站都是初学者。提前:如果我以错误的方式使用本网站,我会道歉。)

(使用C#)

我正试图编写一种计算曼德尔数的方法(目的是在另一个程序中使用该方法),但我收到一条错误消息,上面说:

“错误1‘字符串’不包含接受0个参数的构造函数”

在此之前,“并非所有代码路径都返回值”,但我想我用convert.tostring修复了这一问题。然而,这个错误对我来说是新的

我不知道这意味着什么,谁能解释一下问题是什么

使用制度

命名空间Windows窗体应用程序1{

public class formule
{
    static int Main()
{    
        double f = new double();
        double x = new double();
        double y = new double();
        double a = new double();
        double b = new double();
        int n = new int();
        double max = new double();
        double schaal = new double();
        double afstand = new double();

        x = 0.5;
        y = 0.8;
        n = 0;
        a = 0;
        b = 0;
        max = 100;
        //schaal = invoer 4
        afstand = 0;

        while (afstand <= 2 || n < max)
        {

            afstand = Math.Sqrt((a * a) + (b * b));
            n++;
            a = (a * a - b * b + x);
            b = (2 * a * b + y);


        }
        string mandelgetal;
        mandelgetal = new string();
        mandelgetal = Convert.ToString(n);

        Console.WriteLine(mandelgetal);
        Console.ReadLine();
}






    }

}
公共类公式
{
静态int Main()
{    
double f=新的double();
double x=新的double();
双y=新的双();
双a=新的双();
双b=新的双();
int n=新的int();
double max=新的double();
double schaal=新的double();
double afstand=新的double();
x=0.5;
y=0.8;
n=0;
a=0;
b=0;
最大值=100;
//schaal=发票4
afstand=0;
而(afstand表示“string”不包含接受0个参数的构造函数

如果需要空字符串,请使用
string.empty
初始化变量,或将其指定为null以避免未初始化的变量错误


除此之外,您可以看到
String
类的所有
public
构造函数,并且您会注意到没有无参数构造函数。

您在这一行有一个问题(当然,还有一些其他问题,但阻塞的问题)是

因为字符串中没有不带参数的ctor(在这里,括号之间没有任何内容)

所以你可以这么做

var mandelgetal = string.Empty; 

但实际上,删除这些行会更容易:

string mandelgetal;
mandelgetal = new string();
mandelgetal = Convert.ToString(n);
就这么做吧

    Console.WriteLine(n);
as
Console.WriteLine
可以将int作为参数

编辑

我会用(假设你的算法是正确的)重写所有这些

const double X=0.5;
常数双Y=0.8;
常数双倍最大值=100;
int n=0;
双afstand=0;
双a=0;
双b=0;

虽然(afstand您正在生成大量不必要的代码。请查看下面的必要代码:

公共类公式
{
静态void Main()
{    
双f;
双x=0.5;
双y=0.8;
双a;
双b;
int n;
双倍最大值=100.0;
双schaal;
双支架;

而在此之前,“并非所有代码路径都返回值”,但我想我用convert.ToString解决了这个问题
…不。
double
int
是值类型,您不需要对它们使用
new
。除了答案之外,您的代码中还有一个逻辑错误。您需要存储
a
的旧值,因为您在计算
b
时使用它。如果不这样做,曼德布罗特分形将看起来倾斜d、
a_tmp=a;a=a_tmp*a_tmp-b*b+x;b=2*a_tmp*b+y;
除了您指出的问题外,计算中还有一个逻辑错误。需要存储
a
的旧值,因为它用于计算
a
b
的新值。如果计算不正确,则需要使用Mandelbrot分形al看起来是歪斜的。@RaphaëlAlthaus它们最初被设置为零。因为零已经是默认值,这不会增加任何值。似乎构造函数方法的错误可以修复。但是,我仍然不明白为什么编译器声称并非所有代码路径都返回值。这与“While”有关对吗?但是当我添加时,那部分怎么可能没有结果呢n@A.R.Lifmann如果Main返回
int
,则需要返回一个值。在C#中,返回
void
,这是很常见的,因此您不必为Main的返回值操心。因为您怀疑算法不是100%正确的。
a
的旧值需要重新设置红色,因为它用于更新
a
b
。如果使用不当,Mandelbrot分形将看起来倾斜。
string mandelgetal;
mandelgetal = new string();
mandelgetal = Convert.ToString(n);
    Console.WriteLine(n);
const double X = 0.5;
const double Y = 0.8;
const double Max = 100;

int n = 0;
double afstand = 0;
double a = 0;
double b = 0;

while (afstand <= 2 || n < Max) {

        afstand = Math.Sqrt((a * a) + (b * b));
        n++;
        a = (a * a - b * b + X);
        b = (2 * a * b + Y);
}

Console.WriteLine(n);
Console.ReadLine();
public class formule
  {
    static void Main()
    {    
        double f;
        double x = 0.5;
        double y = 0.8;
        double a;
        double b;
        int n;
        double max = 100.0;
        double schaal;
        double afstand;

        while (afstand <= 2 || n < max)
        {
            afstand = Math.Sqrt((a * a) + (b * b));
            n++;
            a = (a * a - b * b + x);
            b = (2 * a * b + y);
        }

        string mandelgetal = n.ToString();

        Console.WriteLine(mandelgetal);
        Console.ReadLine();
      }
  }