2012-10-31 47 views
0

只是在這裏丟了一點。我的問題是我寫了一個簡單的代碼塊,允許用戶選擇一個水果。該代碼做我想要它做的一個問題,我需要它循環回代碼的開始,以便用戶重新選擇一個可選的水果。C#循環一段代碼(包含一個數組)

但是,我似乎無法得到它的工作,但我懷疑問題是我對循環的理解,這將需要更多的關注。我知道我需要一個初始化器和某種條件來執行循環。我嘗試了很多(瘋狂)的東西,包括試圖執行這樣的循環:for(string [] fruitArray = {「Banana」,「Apple」,「Orange」,「Pineapple」});但編譯器沒有它,我想堆棧溢出社區也不會接受。任何幫助和建議表示讚賞。

{ 

      Console.WriteLine("\n", "\n"); 

      string[] fruitArray = {"Banana", "Apple", "Orange", "Pineapple"}; 

      Console.WriteLine("Please select your fruit: \n\n{0} \n{1} \n{2} \n{3}" + "\n", fruitArray[0], fruitArray[1], fruitArray[2], fruitArray[3]); 
      string selection = Console.ReadLine(); 


      if (selection == fruitArray[0]) 
      { 
       Console.WriteLine("\nYou have selected {0} ", fruitArray[0]); 
      } 
      else if (selection == fruitArray[1]) 
      { 
       Console.WriteLine("\nYou have selected {0} ", fruitArray[1]); 
      } 
      else if (selection == fruitArray[2]) 
      { 
       Console.WriteLine("\nYou have selected {0} ", fruitArray[2]); 
      } 
      else if (selection == fruitArray[3]) 
      { 
       Console.WriteLine("\nYou have selected {0} ", fruitArray[3]); 
      } 
      else 
      { 
       Console.WriteLine("\nSelection not recognised. Please select fruit: "); 
      } 

     } 

     Console.ReadKey(); 

    } 
    } 
} 
+1

嘗試'的foreach(..'與字符串 – RBarryYoung

+2

什麼是退出條件......如果您不再希望選擇更多的水果? – Mate

+0

你綁輸入'for'然後壓片?它應該給你正確語法的骨架 – Brad

回答

1

C#對於http://msdn.microsoft.com/en-us/library/ch45axte.aspx

C#的foreach http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx

string[] fruitArray = { "Banana", "Apple", "Orange", "Pineapple" }; 

     Console.WriteLine("Please select your fruit: \n");    
     foreach (string fruit in fruitArray) 
     { 
      Console.Write("\n{0}", fruit); 
     } 

     Console.WriteLine(); 



      string selection = Console.ReadLine(); 
      for (int index = 0; index < fruitArray.Length; index++) 
      { 
       if (fruitArray[index].Equals(selection)) 
       { 
        Console.WriteLine("\nYou have selected {0} ", fruitArray[index]); 
        break; 
       } 
      } 

      foreach (string fruit in new[] {"Banana", "Apple", "Orange", "Pineapple" }) 
      { 
       if (fruit.Equals(selection)) 
       { 
        Console.WriteLine("\nYou have selected {0} ", fruit); 
        break; 
       } 
      } 
2

循環而不出口

while (true) 
      { 
       Console.WriteLine("\n", "\n"); 

       string[] fruitArray = { "Banana", "Apple", "Orange", "Pineapple" }; 

       Console.WriteLine("Please select your fruit: \n\n{0} \n{1} \n{2} \n{3}" + "\n", fruitArray[0], fruitArray[1], 
            fruitArray[2], fruitArray[3]); 
       string selection = Console.ReadLine(); 

       switch (selection) 
       { 
        case "Banana": 
         Console.WriteLine("\nYou have selected {0} ", fruitArray[0]); 
         break; 

        case "Apple": 
         Console.WriteLine("\nYou have selected {0} ", fruitArray[1]); 
         break; 

        case "Orange": 
         Console.WriteLine("\nYou have selected {0} ", fruitArray[2]); 
         break; 

        case "Pineapple": 
         Console.WriteLine("\nYou have selected {0} ", fruitArray[3]); 
         break;      
       } 
      }   

環路與退出條件

  var isLoop = true; 

      do 
      { 
       Console.WriteLine("\n", "\n"); 

       string[] fruitArray = {"Banana", "Apple", "Orange", "Pineapple"}; 

       Console.WriteLine("Please select your fruit: \n\n{0} \n{1} \n{2} \n{3}" + "\n", fruitArray[0], 
            fruitArray[1], 
            fruitArray[2], fruitArray[3]); 
       string selection = Console.ReadLine(); 


       switch (selection) 
       { 
        case "Banana": 
         Console.WriteLine("\nYou have selected {0} ", fruitArray[0]); 
         break; 

        case "Apple": 
         Console.WriteLine("\nYou have selected {0} ", fruitArray[1]); 
         break; 

        case "Orange": 
         Console.WriteLine("\nYou have selected {0} ", fruitArray[2]); 
         break; 

        case "Pineapple": 
         Console.WriteLine("\nYou have selected {0} ", fruitArray[3]); 
         break; 
        default: 
        isLoop = false; 
        break; 
       } 
      } while (isLoop); 
+0

最好在while語句中創建條件直到用戶完成選擇可選水果 –

+1

我這麼認爲,但他決定的條件:) –

+0

工作得很好,做的工作。需要找到一種方式退出,也許Enviornment.Exit()? – wilbomc

1

對於你的榜樣,這應該是足夠了:

string[] fruitArray = { "Banana", "Apple", "Orange", "Pineapple" }; 

Console.WriteLine("Please select your fruit: \n\n{0} \n{1} \n{2} \n{3}" + "\n", fruitArray[0], fruitArray[1], 
           fruitArray[2], fruitArray[3]); 
string selection = Console.ReadLine(); 

Console.WriteLine("\nYou have selected {0} ", selection); 

也許我失去了一些東西,我會很高興瞭解:)

此外,你可以改善的第一條消息通過迭代陣列:

Console.WriteLine("Please select your fruit: \n"); 

for(int i=0;i<fruitArray.Length;i++) 
{ 
    Console.WriteLine(fruitArray[i]); 
} 

希望它有幫助。

1

下面的代碼應該完全按照你想要的來做。不僅如此,它簡化了甚至使用塊if語句或switch的需要,簡單的Array.FindIndex足以確定輸入是否有效。另外,我添加了一個退出子句,這樣用戶可以在完成時退出應用程序,並消除UI的任何混亂情況。我在每次「迭代」後清除控制檯窗口。

static void Main(string[] args) 
    { 
     var selection = ""; 
     while (selection != "q") 
     { 
      Console.WriteLine(Environment.NewLine); 
      string[] fruitArray = { "Banana", "Apple", "Orange", "Pineapple" }; 

      Console.WriteLine("Please select your fruit (or Q to quit): \n\n{0} \n{1} \n{2} \n{3}", fruitArray[0], fruitArray[1], fruitArray[2], fruitArray[3]); 
      Console.Write(Environment.NewLine + "-> "); 
      selection = Console.ReadLine().ToLower(); 

      // valid option selected 
      var index = Array.FindIndex(fruitArray, (fruit) => fruit.ToLower() == selection); 
      if (index > -1) 
      { 
       Console.Write("\nYou have selected {0}.", fruitArray[index]); 
       ContinuePrompt(); 
      } 
      else if (selection != "q") 
      { 
       Console.Write("\nSelection not recognised."); 
       ContinuePrompt(); 
      } 
     } 
    } 

    static void ContinuePrompt() 
    { 
     Console.Write(" Press any key to continue..."); 
     Console.ReadKey(); 
     Console.Clear(); 
    } 
相關問題