Ahs*_*ain 1 .net c# console console-application
我是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();
}
}
Run Code Online (Sandbox Code Playgroud)
}
现在到了这一点:
if (selectedOption == "y")
{
// howto call static void Main(string[] args) here agin so that the program start itself from the start point
}
Run Code Online (Sandbox Code Playgroud)
我想重新启动程序,如果用户输入"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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它对我没有用,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
Run Code Online (Sandbox Code Playgroud)
然后我试图在点上调用main函数本身
if (selectedOption == "y")
{
// howto call static void Main(string[] args) here agin so that the program start itself from the start point
}
Run Code Online (Sandbox Code Playgroud)
但它在这里给我带来了很多错误,不知道现在该怎么做,任何人都可以帮助我吗?不知道该怎么做这个工作....再次在类中调用"static void Main(string [] args)"将是首选....
Sim*_*ead 10
首先,您的标签不正确.标签的末尾应该有一个冒号字符:..所以你的标签应该是这样的:
StartPoint:
Run Code Online (Sandbox Code Playgroud)
但是:
你应该循环直到满足条件.在这种情况下..条件是不重启:
bool running = true;
while (running) {
/*
* all of your other code
* should go here
*/
if (selectedOption != "y") {
running = false;
}
}
Run Code Online (Sandbox Code Playgroud)
你真的不应该使用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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8239 次 |
| 最近记录: |