2011-07-08 10 views
0

私はプログラムの中に小さなプログラムを作っています。私はジレンマに遭遇しました。私の最初のミニプログラムでは、数字を並べ替えることで、数字から最大の数字を見つけることができます。ユーザーが終了したいかどうかを尋ねます。 "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(); 
    } 
} 

}

+0

それは必要がないよう後藤、リファクタリングコードを使用しないでください。 http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF。理想的には、機能を「逃げる」唯一の方法は、孤立した、そして最終的なreturn文です。 – Jodrell

+0

あなたの競争の項目はコンソールウィンドウに限定されていなければなりませんか? – Jodrell

+0

私は実際に競争に参加していない、私は練習問題をやっているだけですが、私はコンソールにそれを保持しているので、私はUIに集中することが少なく、ロジックについてもっと集中することができます。 – airplaneman19

答えて

4

根本的な問題は、あなたが二回のreadlineを呼んでいるということです。入力された値が最初に取得されると、「はい」と表示されます。2回目に呼び出すと、読み取るデータがないため「」が返されます。

bool exit = false; 
while(!exit) 
{ 
    Console.Write("Your Number: "); 
    //Your main code 
    Console.Write("Do you want to exit? Yes/No: "); 
    if(Console.ReadLine() != "No") 
     exit = true; 
} 

:あなたは変数に同じ入力ストア、それを再利用する必要がある場合、すなわち

string inputVal = Console.ReadLine(); 

私は後藤文を嫌い、多分あなたは、whileループの中のようなものをあなたのコードを再構築でき実際には、exit変数を取り除くことができます。while(true)を実行して、ユーザーがno以外を入力した場合は戻ります。

0

私はいくつかの提案を持っている:

  1. は、読みやすくするために、よりモジュールであるためにあなたのコードを記述します。 Main()メソッドは外部UIループを駆動するだけで、各モジュールは独自のUIを提供します。
  2. goto文を使用しないでください。
  3. if条件内でConsole.Readline()を使用しないでください(「はい」でなければ、2回呼び出されました)。ここで

あなたのコードのリファクタリング私のバージョンです:

class Program { 
    static void DigitRearranger() 
    { 
     string response = ""; 
     int num; 
     do 
     { 
      Console.Clear(); 
      Console.ForegroundColor = ConsoleColor.Yellow; 
      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!"); 
      Console.ResetColor(); 

      Console.Write("Your Number: "); 
      if (!int.TryParse(Console.ReadLine(), out num)) 
      { 
       Console.WriteLine("Not a number. Press any key to continue"); 
       Console.ReadKey(); 
       continue; 
      } 
      //todo: reaarrange the number & print results 
      /*Placeholder code for the moment*/ 
      Console.WriteLine(num); 

      Console.Write("Do you want to exit? Yes/No: "); 
      response = Console.ReadLine(); 

     } while (response.ToLower() != "yes"); 
    } 

    //UI driver only in Main method: 
    static void Main(){ 
     string response = ""; 

     do 
     { 
      Console.Clear(); 
      Console.WriteLine("Welcome to the ACSL Competition Program. Choose a program to begin:"); 
      Console.WriteLine("\n\t1\tDigit Re-arranger"); 
      Console.WriteLine("\tq\tQuit"); 

      Console.Write("\nProgram: "); 

      response = Console.ReadLine(); 
      switch(response) 
      { 
       case "1": 
        DigitRearranger(); 
        break; 
       case "q": 
        break; 
       default: 
        Console.WriteLine("Not a valid program. Press any key to continue"); 
        Console.ReadKey(); 
        break; 
      } 
     } while (response.ToLower() != "q"); 
     Console.ReadLine(); 
    }} 
関連する問題