私は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) 」を再度呼び出すことをお勧めします....