2017-01-24 1 views
0

これはデータ構造とアルゴリズムクラスのためのものです。私たちはバブルソートから始めています。命令は、ランダムでユニークな整数を生成し、講義のソート技法を使用してそれらをソートすることでした。異なるソート手法を追加する必要があります。C#コンソールアプリケーション - elseステートメントからリストにアクセスしようとしています

乱数のリストを生成するために、リストを生成し、フィッシャー・イイツ・アルゴリズムを使用してリストをシャッフルしました。だから私は自分が選んだサイズの私のユニークでソートされたリストを持っています。

ランダムリストを生成した後、リストにアクセスしてBubbleSortを実行する際に問題が発生するため、固執しています。

私はこれを行う方法はありますか?

class Algorithms 
{ 
    static void Main(string[] args) 
    {   

     string response = ""; 

     //Main Console Menu 
     while (response != "exit") 
     { 
      Console.WriteLine("Type help for list of commands"); 
      response = Console.ReadLine(); 
      //List<int> toSort = new List<int>(); 

      if (response.StartsWith("exit")) 
      { 
       Environment.Exit(0); 
      } 

      else if (response.ToLower().StartsWith("help")) 
      { 
       Help(response); 
      } 

      else if (response.ToLower().StartsWith("generate")) 
      { 

      // Shuffle(Generate(response)); 
      // have been using the line above but adding next line for 
      //an idea of my problem 

      List<int> toSort = Shuffle(Generate(response)); 

      } 

      else if (response.ToLower().StartsWith("bubble")) 
      { 
       //This doesn't work and I'm trying to figure out how it can 
       BubbleSort(toSort); 

      }       

     } 
    } 


    //Displays help information 
    public static void Help(string input) 
     { 
     Console.WriteLine("\ngenerate <integer> -- Generates a data set of intended amount of integers\n"+ 
      "algorithm <algorithm type> -- Choose which algorithm to sort data\nexit -- exit application\n"); 
     } 

    //Generates List of integers from 0 to number choosen by user 
    public static List<int> Generate(string size) 
    { 
     int cutString = size.Length - 9; 
     string sizeSubset = size.Substring(9, cutString);    
     List<int> numGen = new List<int>(); 
     int dataSetSize = Convert.ToInt32(sizeSubset); 
     for(int i = 0; i <= dataSetSize; i++) 
     { 
      numGen.Add(i); 
      // Console.WriteLine(numGen[i]);     

     } 

     return numGen;     

    } 

    //Use Fisher-Yates algorithm to shuffle the list. 
    static Random randomize = new Random(); 
    public static List<int> Shuffle(List<int>makeRandom) 
    { 

     List<int> shuffled = new List<int>(); 
     int n = makeRandom.Count; 
     while (n > 1) 
     { 
      n--; 
      int k = randomize.Next(n + 1); 
      int value = makeRandom[k]; 
      makeRandom[k] = makeRandom[n]; 
      makeRandom[n] = value; 
      shuffled.Add(value); 
      Console.WriteLine(value); 

     } 

     return shuffled; 

    } 

    public static void BubbleSort(List<int>input) 
    { 
     for(int i = 0; i <= input.Count; i++) 
     { 
      for (int j = 0; j <= (input.Count - 1); j++) 
      { 
       if (input[j] > input[j + 1]) 
       { 
        int temp = input[j]; 
        input[j] = input[j + 1]; 
        input[j + 1] = temp; 
        Console.WriteLine("hello"); 
       } 

      } 
     } 
    } 

} 
} 
+0

まあ、はい、あなたはリストを生成した - しかし、あなたはどこにでもそれへの参照を格納していない... Main' 'でローカル変数を持っていることについて考えますリストへの参照を格納します。 –

+0

私はC#でどのくらい正確にそれをしますか?それは私が取り組んでいることのようなものですが、実際には答えが見つかりませんでした。 – kirkn0909

+0

さて、私はすでにあなたにヒントを与えました。あなたは 'Main'メソッドにローカル変数が必要です。しかし変数が ''バブル ''のケースでまだ有効であることを確認する必要があるので、 '' while'ループの外側に変数を宣言する必要があります。 –

答えて

2

あなたはelse if (response.ToLower().StartsWith("generate"))コードブロックのスコープ内でリストを定義し、それはそのブロックの外にはアクセスできません。このように、Mainメソッドのスコープに宣言を移動:

static void Main(string[] args) 
{   

    string response = ""; 
    //define your list here. 
    List<int> toSort = new List<int>(); 

    //Main Console Menu 
    while (response != "exit") 
    { 
     Console.WriteLine("Type help for list of commands"); 
     response = Console.ReadLine(); 

     if (response.StartsWith("exit")) 
     { 
      Environment.Exit(0); 
     } 

     else if (response.ToLower().StartsWith("help")) 
     { 
      Help(response); 
     } 

     else if (response.ToLower().StartsWith("generate")) 
     { 
      toSort = Shuffle(Generate(response)); 
     } 

     else if (response.ToLower().StartsWith("bubble")) 
     { 

      List<int> sortedList = BubbleSort(toSort); 
     }       

    } 
} 
+0

助けてくれてありがとう。以前はとても似ていましたが、間違って宣言しました。問題が解決しました!! – kirkn0909

+0

うれしい私は助けることができます。また、私の答えを解決策としてマークすることもできます。感謝の言葉を言ったように:) – Nino

関連する問題