私はプログラムの中に小さなプログラムを作っています。私はジレンマに遭遇しました。私の最初のミニプログラムでは、数字を並べ替えることで、数字から最大の数字を見つけることができます。ユーザーが終了したいかどうかを尋ねます。 "Yes"と応答すると、関数は0を返します。これはmain(string [] args)メソッドで評価されます。私の問題は、ユーザーが「いいえ」と答えるたびに、ミニプログラムは引き続き実行されないということです。ここに私のソースです:メイン()に戻るために関数をエスケープする
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();
}
}
}
それは必要がないよう後藤、リファクタリングコードを使用しないでください。 http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF。理想的には、機能を「逃げる」唯一の方法は、孤立した、そして最終的なreturn文です。 – Jodrell
あなたの競争の項目はコンソールウィンドウに限定されていなければなりませんか? – Jodrell
私は実際に競争に参加していない、私は練習問題をやっているだけですが、私はコンソールにそれを保持しているので、私はUIに集中することが少なく、ロジックについてもっと集中することができます。 – airplaneman19