[英]"Cannot implicitly convert type 'string' to 'int'" error in simple console application
[英]C# Console application, cannot implicitly convert type 'double' to 'string'?
我超級難倒。 我正在嘗試創建一個程序,允許用戶創建“汽車”並聲明所述汽車的品牌、型號和顏色。
代碼未完成(並在下面發布)。 我在汽車類中的每個 getter 和 setter 上都有幾個錯誤。
例如,我收到“無法將類型 'double' 隱式轉換為 'string' 的錯誤。 在讀取return make
和make = paramMake
以及其余的 getter 和 setter 的代碼行上。
有任何想法嗎?
namespace Program_4cs
{
class Automobile
{
//2.
//a). PDDC
public Automobile()
{
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("a). Automobile PDDC set.");
}
//PDC
public Automobile(string paramMake, string paramModel, string paramColor)
{
string make = paramMake;
string model = paramModel;
string color = paramColor;
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("a). Automobile PDC set.");
}
//b). Getters and Setters
public string GetMake()
{
return make;
}
public void SetMake(string paramMake)
{
make = paramMake;
}
public string GetModel()
{
return model;
}
public void SetModel(string paramModel)
{
model = paramModel;
}
public string GetColor()
{
return color;
}
public void SetColor(string paramColor)
{
color = paramColor;
}
//c). Speed changing members
public double GetSpeed()
{
return speed;
}
public void SetSpeed(double paramSpeed)
{
speed = paramSpeed;
}
public void IncreaseSpeed(double paramIncreaseSpeedBy)
{
speed = speed + paramIncreaseSpeedBy;
}
public void DecreaseSpeed(double paramDecreaseSpeedBy)
{
speed = speed - paramDecreaseSpeedBy;
}
public void ShowSpeed(double paramShowSpeed)
{
speed = paramShowSpeed;
}
//d). instance members
private double speed;
private double make;
private double model;
private double color;
}
class Program
{
static void Main(string[] args)
{
//1.
//Ouput a header in the console
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("1. This is Program-4.");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Press any key to continue.");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("How many automobiles would you like to create?");
Console.ForegroundColor = ConsoleColor.White;
int numOfAutomobiles = int.Parse(Console.ReadLine());
Automobile[] listOfAutomobiles = new Automobile[numOfAutomobiles];
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Please input model, and color");
Console.ForegroundColor = ConsoleColor.White;
for (int i = 0; i < listOfAutomobiles.Length; i++)
{
Console.WriteLine("Please enter the make: ");
String make = Console.ReadLine();
Console.WriteLine("Please enter the model: ");
String model = Console.ReadLine();
Console.WriteLine("Please enter the color: ");
string color = Console.ReadLine();
Automobile newCar = new Automobile(make, model, color);
listOfAutomobiles[i] = newCar;
}
Console.ReadKey();
}
}
}
先感謝您!
當您創建實例變量private double make
您告訴程序make
的類型為double
。 在SetModel(string paramMake)
您接收一個字符串並嘗試將其設置為雙SetModel(string paramMake)
值。 字符串是一系列字符,而雙精度值是浮點值。 編譯器不能自己轉換這些,這就是給你錯誤的原因(類似的錯誤出現在代碼的其他地方)。
為了解決這個問題,我們可以將 make 存儲為字符串而不是雙精度值。 為此,我們將private double make
更改為private string make
。 我們的另一個選擇是解析字符串以獲得雙精度值。 我們可以用Convert.ToDouble()
做到這一點。
我認為您應該在這里考慮的一個問題是,double 還是 string 是您使用的最佳選擇。 將模型和顏色存儲為雙精度值似乎可以通過字符串更好地完成,而速度適合雙精度值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.