3

私はC#が初めてで、コンソールアプリケーションに取り組んでいます。今日、次のコードを書きました:

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

         }    

しかし、ここで多くのエラーが発生します。今それを行う方法がわかりません。誰か助けてもらえますか? この作業を行う方法がわからない....クラスで「 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 を再度呼び出したり (再帰) するべきではありません。

    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

メインクラスの外で実行するコードを次のような別のメソッドに入れてみてください

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 に答える
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 に答える
0

このようなもの :

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

                 do 
                 {

                               ...........

                 }
                 while (selectedOption == "y")

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

             } 
于 2013-10-29T10:48:05.333 に答える