gpt4 book ai didi

c# - C# 中的 CS0649 错误

转载 作者:太空狗 更新时间:2023-10-30 01:14:23 25 4
gpt4 key购买 nike

代码中有一个我找不到的错误,一些变量根本无法正常工作。

我收到的警告:

CS0649 Field 'Calculations.A' is never assigned to, and will always have its default value 0 ABC-Formule [path]\Calculations.cs

CS0649 Field 'Calculations.B' is never assigned to, and will always have its default value 0 ABC-Formule [path]\Calculations.cs

CS0649 Field 'Calculations.C' is never assigned to, and will always have its default value 0 ABC-Formule [path]\Calculations.cs

代码:程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC_Formule
{
class Program
{
static void Main(string[] args)
{
bool keepGoing = true;
while (keepGoing)
{
//Definieren strings gebruikt in Main
string a;
string b;
string c;
string keepGoingString;
string discriminantString;

double aDouble;
double bDouble;
double cDouble;

double discriminantValue;
double singleX;
double doubleX1;
double doubleX2;

//Vraag de user om input
Console.WriteLine("Welkom bij de ABC-Calculator. Gemaakt door Luciano Nooijen.");
Console.WriteLine("Zorg voor een correcte invoer. De opbouw van een formule is ax^2 + bx + c.");
Console.WriteLine("");
Console.WriteLine("Vul de juiste variabelen in: (komma = ,)");
Console.Write("a: "); a = Console.ReadLine();
Console.Write("b: "); b = Console.ReadLine();
Console.Write("b: "); c = Console.ReadLine();

//Omzetten van string naar double
aDouble = Convert.ToDouble(a);
bDouble = Convert.ToDouble(b);
cDouble = Convert.ToDouble(c);

Console.WriteLine("a, b en c zijn: " + aDouble + ", " + bDouble + " en " + cDouble);
Console.WriteLine("");

//Invoeren getallen in berekenfunctie
Calculations berekeningen = new Calculations(aDouble, bDouble, cDouble);

//Discriminant berekenen
discriminantValue = berekeningen.Discriminant();

//Validatie discriminant en output
discriminantString = berekeningen.ValidDiscriminant();
if (discriminantString == "Gelijk") //Enkele output
{
singleX = berekeningen.OutputOnlyX();
Console.WriteLine("De discriminant is: " + discriminantValue);
Console.WriteLine("Het x-coördinaat is: " + singleX);
}
else if (discriminantString == "Groter") //Dubbele output
{
doubleX1 = berekeningen.OutputX1();
doubleX2= berekeningen.OutputX2();
Console.WriteLine("De discriminant is: " + discriminantValue);
Console.WriteLine("Het x1-coördinaat is: " + doubleX1);
Console.WriteLine("Het x2-coördinaat is: " + doubleX2);
}
else if (discriminantString == "Kleiner") //Geen snijpunt
{
Console.WriteLine("De discriminant is: " + discriminantValue + " en er is dus geen snijpunt met de x-as.");
}
else //Ongeldige invoer
{
Console.WriteLine("Ongeldige invoer");
}

//Vragen of gebruiker door wil gaan
Console.Write("Druk op enter om door te gaan, of druk op typ q om af te sluiten: "); keepGoingString = Console.ReadLine();
if (keepGoingString.Equals("q"))
{
keepGoing = false;
}
else
{
Console.WriteLine("");
Console.WriteLine("-----------------------------------------------------------");
Console.WriteLine("");
Console.Clear();
}
}

}
}
}

计算.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC_Formule
{
class Calculations
{
protected double A;
protected double B;
protected double C;
protected double DiscriminantValue;

public Calculations (double a, double b, double c)
{
double A = a;
double B = b;
double C = c;
}

public double Discriminant ()
{
DiscriminantValue = B*B-4*A*C;
return DiscriminantValue;
}

public string ValidDiscriminant()
{
if (DiscriminantValue < 0) //Geen uitkomst
{
return "Kleiner";
}
else if (DiscriminantValue == 0) //1 uitkomst
{
return "Gelijk";
}
else if (DiscriminantValue > 0) //Twee uitkomsten
{
return "Groter";
}
else //Bij fout
{
return "Error";
}

}

public double OutputOnlyX ()
{
return (-B + Math.Sqrt(DiscriminantValue) ) / (2 * A);
}

public double OutputX1 ()
{
return (-B - Math.Sqrt(DiscriminantValue)) / (2 * A);
}

public double OutputX2 ()
{
return (-B + Math.Sqrt(DiscriminantValue)) / (2 * A);
}
}
}

最佳答案

Calculations 构造函数中你有:

double A = a;

你想要:

A = a; // or this.A = a;

相反(这当然也涉及BC)

在您的代码中,您创建了一个本地(在构造函数的范围内)变量 A,它与类的 A 字段无关(除了有,令人困惑, 同名)。在这种情况下发生的事情称为 shadowing ,有效地导致此函数代码中的每个 A 都表示局部变量,而不是父作用域中的字段。

关于c# - C# 中的 CS0649 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43880714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com