正如其他人在评论中所说的那样,您应该避免使用goto
声明,因为它们是不好的练习,特别是在您正在学习大学编程课程时(通常应该符合结构编程)。相反,使用while
循环(或任何其他)有两个条件,如在示例中所示。另外,我认为你应该从较小的数字开始搜索(第一次输入不需要更小),这在性能方面略有改进。这是代码:
static void Main(string[] args)
{
string myInput;
int myInt;
string myInput2;
int myInt2;
int i;
Console.Write("Please enter a number: ");
myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);
Console.Write("Please enter another number: ");
myInput2 = Console.ReadLine();
myInt2 = Int32.Parse(myInput2);
i = myInt > myInt2 ? myInt2 : myInt;
bool found = false;
while(!found && i>0)
{
if (myInt % i == 0 && myInt2 % i == 0)
{
Console.Write("Your GCF is...");
Console.Write("{0} ", i);
Console.ReadLine();
found = true;
}
else
i--;
}
}
编辑:我包括其他可能的解决方案由于@Servy
bool found = false;
for(i = Math.Min(myInt, myInt2); !found && i>0; i--)
{
if (myInt % i == 0 && myInt2 % i == 0)
{
Console.Write("Your GCF is...");
Console.Write("{0} ", i);
Console.ReadLine();
found = true;
}
}
转到?我想我只是在嘴里吐了一下。 –
这是功课吗?第二次删除goto! –
那么有什么不工作?它是否会产生错误,你是否得到误报,没有答案,它是否永远运行,是否给出了错误的答案,它只是有时工作,你是否真的没有运行它,它不会编译,[...]? – Servy