代码中存在一个我无法找到的错误,某些变量根本无法以某种方式工作。
我得到的警告:
CS0649字段'计算.A'永远不会被赋予,并且将始终具有其默认值0 ABC-Formule [path] \ Calculations.cs
CS0649字段'计算.B'永远不会被赋予,并且将始终具有其默认值0 ABC-Formule [path] \ Calculations.cs
CS0649字段'计算.C'永远不会被赋予,并且将始终具有其默认值0 ABC-Formule [path] \ Calculations.cs
代码: Program.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();
}
}
}
}
}
Calculations.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);
}
}
}
答案 0 :(得分:4)
在Calculations
构造函数中,您有:
double A = a;
你想要:
A = a; // or this.A = a;
代替(当然这也涉及B
和C
)。
在您的代码中,您创建一个本地(在构造函数的范围内)变量A
,该变量与该类的A
字段无关(除了令人困惑之外,一样的名字)。在这种情况下发生的事情称为shadowing,有效地导致此函数代码中的每个A
表示局部变量,而不是父范围中的字段。