我在C#中新的控制檯應用程序的工作現在是一個天我寫了下面的代碼:如何調用「靜態無效的主要(字串[] args)」中的類再次
Program.cs中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch06Ex01
{
class Program
{
static void Write()
{
Console.WriteLine("Please enter any string..!!");
}
static void Main(string[] args)
{
Write();
string name = Console.ReadLine();
Write();
string name1 = Console.ReadLine();
Write();
string name2 = Console.ReadLine();
Write();
string name3 = Console.ReadLine();
Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);
Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
string selectedOption = Console.ReadLine();
if (selectedOption == "y")
{
// howto call static void Main(string[] args) here agin so that the program start itself from the start point
}
//else if (selectedOption == "n")
{
//Terminate the Program
}
Console.ReadKey();
}
}
}
現在在點:
if (selectedOption == "y")
{
// howto call static void Main(string[] args) here agin so that the program start itself from the start point
}
我想重新啓動程序,如果用戶輸入「Y」並終止它,如果用戶輸入「N」,所有這一切的目的冷杉我試圖用像goto語句:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch06Ex01
{
class Program
{
static void Write()
{
Console.WriteLine("Please enter any string..!!");
}
static void Main(string[] args)
{
StartPoint;
Write();
string name = Console.ReadLine();
Write();
string name1 = Console.ReadLine();
Write();
string name2 = Console.ReadLine();
Write();
string name3 = Console.ReadLine();
Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);
Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
string selectedOption = Console.ReadLine();
if (selectedOption == "y")
{
// howto call static void Main(string[] args) here agin so that the program start itself from the start point
goto StartPoint;
}
//else if (selectedOption == "n")
Console.ReadKey();
}
}
}
但事與願違在StartPoint;
工作對我來說它給出錯誤
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement C:\Users\Ahsan\Desktop\C# for Beginners Examples\Ch06Ex01\Ch06Ex01\Program.cs 18 13 Ch06Ex01
然後我試着打電話給主函數本身在點
if (selectedOption == "y")
{
// howto call static void Main(string[] args) here agin so that the program start itself from the start point
}
,但在這裏給了我很多的錯誤,不要懂得d現在呢,任何人都可以幫助我嗎?不知道如何做這個事情.... 調用「靜態無效主(字符串[] ARGS)」在該班再次將首選....
你可以用'而(Console.ReadKey() !=''){code here}' –
kravasb
Static void Main(string [] args)是C#控制檯應用程序或Windows應用程序的入口點。當應用程序啓動時,Main方法是第一個被調用的方法。你爲什麼想再回想一遍? – Doro
雖然人們給你一個答案(如何調用你自己的程序根目錄),但我會小心這樣做。直到線程遍歷Main的完整代碼塊,您的應用程序纔會結束。在這種情況下,直到內部'main'完成後,第一次調用'Main'纔會被解析。如果你不斷地這樣做,你可能會冒着堆棧溢出的風險。但是,具有「空」返回值的'Main'可能會導致一些尾部呼叫優化的發生。 while循環是這個問題的慣用C#方法。 –