3

我是 C# 的新手,现在正在处理控制台应用程序,我写了以下代码:

程序.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

         }    

但这给了我很多错误,现在不知道该怎么做,有人可以帮助我吗?不知道如何做这件事......再次在类中调用“ static void Main(string[] args) ”将是首选......

4

5 回答 5

10

首先,您的标签不正确。标签的结尾应该有一个冒号字符:.. 所以你的标签应该是这样的:

StartPoint:

但是

您应该循环直到满足条件。在这种情况下..条件是不重新启动:

bool running = true;

while (running) {
    /* 
     * all of your other code
     * should go here
     */
    if (selectedOption != "y") {
        running = false;
    }
}
于 2013-10-29T10:39:40.797 回答
5

您真的不应该使用 goto 或再次调用 Main(递归),do while 是重复逻辑多次的更好解决方案:

    string selectedOption;
    do {
        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");
         selectedOption = Console.ReadLine();
      } while (selectedOption == "y")
      Console.ReadKey();
于 2013-10-29T10:44:06.953 回答
1

只需再次调用该方法,传入与最初传入的参数相同的参数,如果可以的话,也尽量避免使用 goto

if (selectedOption == "y")
{
    // howto call  static void Main(string[] args) here agin so that the program start itself from the start point
    Main(args);
}    
于 2013-10-29T10:40:29.470 回答
1

尝试将您将在主类之外执行的代码放在另一种方法中,例如

void execute()
{
        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();

}

然后在主要

static void Main(string[] args)
{
    bool run = true
    while(run){
       execute()
       Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
        selectedOption = Console.ReadLine();
        if (selectedOption == "n")
        {
             run = false;
        }    
    }
}
于 2013-10-29T10:48:25.050 回答
0

像这样的东西:

static void Main(string[] args)
            {      
                string selectedOption = "";

                 do 
                 {

                               ...........

                 }
                 while (selectedOption == "y")

                 if (selectedOption == "n")
                 {
                   //Terminate the Program
                  }

             } 
于 2013-10-29T10:48:05.333 回答