如何在c#中的特定行上重新启动?
我是个初学者,我试着用c#创建一个简单的计算器。我想,当你完成一个操作,你可以重新启动或不。 这是我的密码:如何在c#中的特定行上重新启动?,c#,C#,我是个初学者,我试着用c#创建一个简单的计算器。我想,当你完成一个操作,你可以重新启动或不。 这是我的密码: using System; namespace Calculator { class Program { static void Main(string[] args) { // First number Console.WriteLine("Enter a number");
using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
// First number
Console.WriteLine("Enter a number");
double A = Convert.ToDouble(Console.ReadLine());
// Second number
Console.WriteLine("Enter another number");
double B = Convert.ToDouble(Console.ReadLine());
// Operator
Console.WriteLine("Enter the operator");
string C = Console.ReadLine();
// if you want to add
if(C == "+")
{
Console.WriteLine(A + B);
}
// if you want to remove
if(C == "-")
{
Console.WriteLine(A - B);
}
// if you want to multiply
if(C == "*")
{
Console.WriteLine(A * B);
}
// if you want to subdivide
if(C == "/")
{
Console.WriteLine(A / B);
}
// Ask if they want to restart or finish
Console.WriteLine("Want to do another operations? y/n");
string W = Console.ReadLine();
// Restart
if(W == "y")
{
// Return at the beginning
}
// Finish
if(W == "n")
{
Console.WriteLine("Enter a key to close");
Console.ReadKey();
}
}
}
}
在这里你可以看到,当你完成你的操作,你可以重新启动(这是一个我不知道如何)或完成。
我的代码(和演讲)效率不高(我是意大利人)
我不擅长编程,我正在努力自学。你的问题的具体答案是:
goto
您放置一个标签myLabel:
,然后当您想跳到那里时,您可以转到myLabel代码>
但是,goto是邪恶的,必须避免,在大型程序中,它使代码无法读取,并导致大量问题
好的解决方案是创建一个循环并测试一个变量,如下所示:
bool execute = true;
while(execute)
{
//..your calculator code
Console.WriteLine("Want to do another operations? y/n");
string W = Console.ReadLine();
if(W == "n")
execute = false;
}
这使代码更加清晰易读。对于您的问题,如何跳转到具体行的具体答案是:goto
您放置一个标签myLabel:
,然后当您想跳到那里时,您可以转到myLabel代码>
但是,goto是邪恶的,必须避免,在大型程序中,它使代码无法读取,并导致大量问题
好的解决方案是创建一个循环并测试一个变量,如下所示:
bool execute = true;
while(execute)
{
//..your calculator code
Console.WriteLine("Want to do another operations? y/n");
string W = Console.ReadLine();
if(W == "n")
execute = false;
}
这使代码更加清晰易读。将所有代码放在一个循环中,检查循环。我同意。在这里使用while循环似乎很好。把它全部放在一个循环中,检查循环。我同意。在这里使用while循环似乎很好。