2014-04-08 51 views
1

您好,我很新的C#所以請容忍我,如果我的問題有一個很明顯的答案。從陣列中輸入查詢選擇C#

我想寫顯示一組選項給用戶以數字來選擇選項的代碼。用戶然後選擇他們的選項,代碼將顯示他們從數組中的選擇。

因此,例如

Select 1 for apples 
Select 2 for oranges 
Select 3 for pears 

Please Enter your Selection : 

我一直鍵入一個for循環來顯示我的領域,但我不能讓我的程序從陣列讀取此輸入的是我迄今爲止

static void Main(string[] args) { 

    string[] fruit = [3] 

    fruit[0]=("apples"); 
    fruit[1]=("oranges"); 
    fruit[2]=("pears"); 

    for (int i = 0; i < fruit.length; i++) 
    { 
    Console.WriteLine("Enter {0} to select {1}", i, fruit[i]); 
    } 

    string choice = Console.ReadLine(); 
    int i = int.Parse(choice); 
    Console.WriteLine("You have selected {0} which is {1}", i, fruit[i]); 
    Console.ReadLine(); 
} 

這給了我一個錯誤,如果我把這個內部的for循環,則程序不顯示我所有的選項。

+0

我知道我錯過了我的括號內爲主要方法我打字快問這個問題 – user3511925

+1

有一個編輯按鈕 –

+0

是我;在尋找,但無法找到它 – user3511925

回答

3

您需要在您的循環變量或用戶的選擇給出不同的名字。你

可能還需要使用ParseTryParse這一翻譯,以防止可能FormatExceptions

int userChoice; 
if(int.TryParse(choice, out userChoice) && userChoice < fruit.Length) 
     Console.WriteLine("You have selected {0} which is {1}", userChoice, fruit[userChoice]); 
+0

謝謝爲你的幫助,我會盡力 – user3511925

2
void Main() 
{ 
    // compile a list of possible fruits (remember that c# arrays are 
    // 0-indexed (first element is actually 0 and not 1)) 
    string[] fruits = new[]{ 
     "apples", 
     "oranges", 
     "pears" 
    }; 

    // iterate over the options and output each one 
    for (var i = 0; i < fruits.Length; i++){ 
     // add one to index value to make it non-0 based 
     Console.WriteLine("Enter {0} to select {1}", i + 1, fruits[i]); 
    } 

    // continue to read user input until they select a valid choice 
    Int32 choice = 0; 
    do 
    { 
     // read user input 
     String input = Console.ReadLine(); 
     // [try to] convert the input to an integer 
     // if it fails, you'll end up with choice=0 
     // which will force the while(...) condition to fail 
     // and, therefore, retry for another selection. 
     Int32.TryParse(input, out choice); 
    } 
    // number should be between 1 and (fruits.Length + 1) 
    while (choice < 1 || choice > (fruits.Length + 1)); 

    // to reference it, subtract 1 from user's choice to get back to 
    // 0-indexed array 
    Console.WriteLine("You have selected {0} which is {1}", choice, fruits[choice - 1]); 
} 
0

您的字符串數組聲明不正確。嘗試

string[] fruit = new string[3]; 

,或者你可以聲明,並啓動這樣的:

string[] fruit = new string[3]{"apples", "oranges", "pears"}; 

或簡單:

string[] fruit = {"apples", "oranges", "pears"}; 
1

您有多個錯別字,你不能使用變量i兩次。

試試這個:

static public void Main(string[] args) 
{ 

    string[] fruit = new string[3]; 

    fruit[0]=("apples"); 
    fruit[1]=("oranges"); 
    fruit[2]=("pears"); 

    for (int i = 0; i < fruit.Length; i++) 
    { 
    Console.WriteLine("Enter {0} to select {1}", i, fruit[i]); 
    } 

    string choice = Console.ReadLine(); 
    int j = int.Parse(choice); 
    Console.WriteLine("You have selected {0} which is {1}", j, fruit[j]); 
    Console.ReadLine(); 
} 

您可能還需要包括某些錯誤捕獲如果用戶輸入的東西,不能被(你可以使用TryParse例如)解析爲int

錯別字:您的數組的聲明是錯誤的,你需要的長度captial L爲數組長度屬性。

+0

謝謝你這幫助了很多 – user3511925

4

多個問題與您的代碼:

  1. 定義你的字符串數組一樣string[] fruit = new string[3];
  2. 既然你已經在你的循環,你需要使用一個新的變量您的輸入定義i
  3. 你在for循環條件應該是i < fruit.Length
  4. 這更是一個建議的,使用int.TryParse從控制檯解析輸入,檢查是否輸入的號碼是一個int並檢查該號碼是否小於陣列長度。