我已經被賦予了重寫一個使用goto語句的程序的任務,這個程序的唯一目的是證明它們有多令人沮喪,並且我已經瞭解到他們確實非常令人沮喪。經過幾個小時翻閱調試器後,我仍然遇到問題,有沒有人可以爲我編寫僞代碼,以便我可以更好地理解它?除了爲每個goto創建if和else語句之外,我都迷路了。用於GoTo程序的C#Sudo代碼
namespace Assignment_03_Nasty_Code
{
class Assignment03
{
static void Main(string[] args)
{
Console.WriteLine("Assignment 03 = " + new Assignment03().Solve());
}
public int Solve()
{
int result = 0;
int upperSearchLimit = 1_000_000;
Console.WriteLine("Building list of primes...");
ArrayList primes = Utils.BuildListOfPrimes(upperSearchLimit);
Console.WriteLine(" Done.");
// Step through the odd composite numbers
for (int i = 19; i < upperSearchLimit; i+= 2)
{
Label02:
if (primes.Contains(i))
goto Label03;
// Is the number divisible by a prime?
int j = 0;
Boolean match = false;
Label01:
int tmp;
int prime = (int)primes[j];
tmp = i - prime;
int half = tmp /= 2;
int squareRoot = (int) Math.Sqrt(half);
if (tmp != squareRoot * squareRoot)
goto Label04;
// We got one
//System.out.println(i + " is a composite that can be written as the sum of a prime and twice a square");
match = true;
Console.WriteLine(i + " = " + (int)primes[j] + " + 2 * " + half);
Label04: // Second goto
j++;
if ((int)primes[j] < i)
goto Label01;
if (match == false)
{
Console.WriteLine("No match for " + i);
result = i;
break;
}
//goto Label02;
Label03: // First goto
int x;
}
return result;
}
}
}
我做的第一件事將通過格式化來運行代碼! –
這種打敗的目的,然後我想我的目的是教我從來沒有使用goto聲明,以及我不需要告訴爲什麼在此之後。 – shockemc
他的意思是修正縮進,「文本牆」不必要的困難,因爲它在可維護性方面存在多個問題,「goto」語句只是其中一個問題。首先修復縮進,然後開始處理goto。 –