怎么称呼静态void Main(string [] args)“再次在类中 [英] how to call " static void Main(string[] args) "in the class again
问题描述
我是C#的新手,现在有一天我编写了以下代码来处理控制台应用程序:
i am new in C# and working on Console Applications now a days i wrote the following code :
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语句:
i wanted to reboot the program if User enter "y" and terminate it if the user enter "n", for this purpose firs of all i tried to use goto statement like :
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
然后我尝试在点处调用主函数本身
then i tried to call main function itself at point
if (selectedOption == "y")
{
// howto call static void Main(string[] args) here agin so that the program start itself from the start point
}
但是它给了我这里有很多错误,现在不知道该怎么做,有人可以帮助我吗?不知道该怎么做。...
再次在该类中调用 static void Main(string [] args)……。
but it gives me a lot of error here , don' know how to do it now, can any one help me out ? don't know how to do this thing work.... calling " static void Main(string[] args) "in the class again would be preferred....
推荐答案
只需再次调用该方法,并传递与最初传递的参数相同的参数,也如果可以的话,尽量避免使用goto :
Just call the method again, passing in the same arguments that where passed in initially, also try to avoid using goto if you can:
if (selectedOption == "y")
{
// howto call static void Main(string[] args) here agin so that the program start itself from the start point
Main(args);
}
这篇关于怎么称呼静态void Main(string [] args)“再次在类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!