我對C#很陌生,但我每天都在學習越來越多。今天,我試圖構建一個簡單的控制檯計算器,並需要幫助將函數中的變量傳遞給Main(),以便我可以在if-else中使用它來確定執行哪個函數。通過函數傳遞變量到main()
public static void Main(string[] args)
{
int decision = Introduction();
Console.Clear();
Console.WriteLine(decision);
Console.ReadLine();
}
public static int Introduction()
{
int decision = 0;
while (decision < 1 || decision > 7)
{
Console.Clear();
Console.WriteLine("Advanced Math Calculations 1.0");
Console.WriteLine("==========================");
Console.WriteLine("What function would you like to perform?");
Console.WriteLine("Press 1 for Addition ++++");
Console.WriteLine("Press 2 for Subtraction -----");
Console.WriteLine("Press 3 for Multiplication ****");
Console.WriteLine("Press 4 for Division ////");
Console.WriteLine("Press 5 for calculating the Perimeter of a rectangle (x/y)");
Console.WriteLine("Press 6 for calculating the Volume of an object (x/y/z)");
Console.WriteLine("Press 7 for calculating the standard deviation of a set of 10 numbers");
decision = int.Parse(Console.ReadLine());
if (decision < 1 || decision > 7)
{
decision = 0;
Console.WriteLine("Please select a function from the list. Press Enter to reselect.");
Console.ReadLine();
}
else
{
break;
}
}
return decision;
}
當我嘗試使用主要決定了(),它說:「這個名字決定不會在當前的背景下存在」。
我很難過,並嘗試使用谷歌搜索無濟於事。
乾杯
SUCCESS!
爲什麼你需要在'Main'和'Introduction'之間共享'決定'? – ebb