0
我想做一個程序,你爲int輸入一個值,然後使用方法從該整數中加上或減去數字「10」。問題是,當我寫:Console.WriteLine("After addition, your value is now {0}",CustomClass.CustomClass'Method);
編譯器告訴我,我不能將對象轉換爲方法。 這裏是我的代碼:WriteLine方法C#
public class CarClass
{
public static string carModel;
public static int carSpeed;
public static void Accelerate()
{
carSpeed = carSpeed + 10;
}
public static void Brake()
{
carSpeed = carSpeed - 10;
}
}
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Enter car model: ");
CarClass.carModel = Console.ReadLine();
Console.WriteLine("Enter initial speed: ");
CarClass.carSpeed = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Starting speed of {0} is {1} km/h.", CarClass.carModel, CarClass.carSpeed);
Console.WriteLine("Now some speed changing...");
Console.WriteLine("After accelerating, new speed of {0} is {1} km/h.",CarClass.carModel,CarClass.carSpeed + CarClass.Accelerate); // here the speed should be starting speed + 10
Console.WriteLine("After accelerating, new speed of {0} is {1} km/h.",CarClass.carModel,CarClass.carSpeed + CarClass.Accelerate); //here the speed should be the previous speed + 10
Console.ReadLine();
}
}
}
我不知道這是否是使加速的正確方法,因爲我需要它來存儲以前的值,然後重新添加「10」。 如果你能幫我解決這個問題,我會非常感謝你們。 在此先感謝。
非常感謝你,這是我需要的東西! –
您的回答解釋了問題,解決問題的另一種方法是在「制動」或「加速」後返回速度。 return carSpeed; } public static int Brake() { carSpeed = carSpeed - 10; return carSpeed; } – Luc