我正在製作一個程序,裏面有很少的程序,我已經走到了一個兩難的境地。在我的第一個微型程序重新排列數字以從這些數字中找到最大可能的數字時,它會詢問用戶是否想要退出。如果他們回答「是」,則該函數返回0,該值在main(string [] args)方法中計算。我的問題是,無論何時用戶說「不」,小程序仍然不會繼續。下面是我的源:轉義一個函數返回Main()
namespace ACSL_Competition
{
class Program
{
static int DigitRearranger()
{
string[] mainString = {};
Console.WriteLine("---------Welcome to the Digit Re-arranger!---------");
Console.WriteLine("I take a positive number up to 10000, and find the highest number that can be made out of its digits.");
Console.WriteLine("Instructions: Enter a number up to 10000, and see the results!");
drLabel:
Console.Write("Your Number: ");
string input = Console.ReadLine();
int inputNumber = 0;
try { inputNumber = int.Parse(input); }
catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); goto drLabel; }
/*Placeholder code for the moment*/Console.WriteLine(inputNumber.ToString());
evaluate:
Console.Write("Do you want to exit? Yes/No: ");
if (Console.ReadLine().Equals("Yes"))
return 1;
else if (Console.ReadLine().Equals("No"))
{
goto drLabel;
}
else
{
return 1;
}
}
static void Main(string[] args)
{
Console.WriteLine("Welcome to the ACSL Competition Program. Choose a program to begin:");
Console.Write("\n\t");
Console.WriteLine("1\tDigit Re-arranger");
label:
Console.Write("\nProgram: ");
string input = Console.ReadLine();
int number = 0;
try { number = int.Parse(input); }
catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); goto label; }
if (number == 1)
{
Console.WriteLine("\n");
if (DigitRearranger() == 1)
{
goto label;
}
else if (DigitRearranger() != 1)
{
DigitRearranger();
}
}
else if (!number.Equals(1))
{
Console.WriteLine("Not a valid program.");
goto label;
}
//----------------
Console.ReadLine();
}
}
}
不要使用goto語句,重構代碼。 http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF。理想情況下,您應該「逃避」您的功能的唯一方法是在單獨和最終的退貨聲明。 – Jodrell
您的參賽作品是否必須限制在控制檯窗口? – Jodrell
我實際上並沒有進入比賽,我只是在練習中遇到問題,但我將它保存在控制檯中,這樣我就可以更少地關注用戶界面,更多關注邏輯。 – airplaneman19