2016-08-19 87 views
0

我有一個用戶輸入一天(星期一)和一個星期的數字,以搜索數組中的值。但是,要獲得數組輸出,我需要行數和列數。行號可以從用戶輸入1,2等獲得。但是,他們輸入星期一,星期二等的部分有點棘手。有沒有辦法將星期一星期二等轉換爲整數(1,2),以便我可以使用它來查找數組元素?如何將星期一的星期一轉換爲整數1,星期二轉換爲星期二的星期二?

我試過這種方法,但它不工作。由於「星期一」不是數字,因此將值轉換爲int顯然不起作用。有沒有其他的方法來做到這一點?

int Value = Convert.ToInt32(value); 

DateTime ClockInfoFromSystem = DateTime.Now; 
Value = (int)ClockInfoFromSystem.DayOfWeek; 

這是代碼的其餘部分,如果你需要它

private void AddToArray() 
{ 
    txtOutput.Text = "Filling the array with user input..." + "\r\n\r\n"; 

    String value; 
    int num; 

    for (int week = 0; week < productsArray.GetLength(0); week++) 
    { 
     for (int day = 0; day < productsArray.GetLength(1); day++) 
     { 
      value = Microsoft.VisualBasic.Interaction.InputBox("Enter Value for Day " + day + " of Week " + week, "Enter Value"); 
      try 
      { 
       while (!(int.TryParse(value, out num))) 
       { 
        MessageBox.Show("Not a valid number, try again."); 
        value = Microsoft.VisualBasic.Interaction.InputBox("Enter a Number", "Enter Number"); 

       } 
      } 
      catch (Exception) 
      { 
       MessageBox.Show("Value entered is not in a valid format"); 
      } 

      productsArray[week, day] += int.Parse(value); 
     } 
    } 
    txtOutput.Text += "The product allocation is as follows:" + "\r\n\r\n"; 
} 

private void Array() 
{ 
    txtOutput.Text += "\tMon\tTue\tWed\tThu\tFri\r\n"; 
    for (int week = 0; week < productsArray.GetLength(0); week++) 
    { 
     txtOutput.Text += "Week " + (week + 1) + "\t"; 
     for (int day = 0; day < productsArray.GetLength(0); day++) 
     { 
      txtOutput.Text += productsArray[week, day] + "\t"; 
     } 
     txtOutput.Text += "\r\n"; 
    } 

    txtOutput.Text += "\r\n" + "Retrieve products completed on a specific day." + "\r\n"; 
    String value = Microsoft.VisualBasic.Interaction.InputBox("Enter Day", "Enter Day", "Monday"); 

    int num; 

    String value2 = Microsoft.VisualBasic.Interaction.InputBox("Enter Week", "Enter Week"); 
    try 
    { 
     while (!(int.TryParse(value2, out num))) 
     { 
      MessageBox.Show("Not a valid number, try again."); 
      value2 = Microsoft.VisualBasic.Interaction.InputBox("Enter Week", "Enter Week"); 
     } 
    } 
    catch (Exception) 
    { 
     MessageBox.Show("Value entered is not in a valid format"); 
    } 
    int Value2 = Convert.ToInt32(value2); 
    int Value = Convert.ToInt32(value); 

    DateTime ClockInfoFromSystem = DateTime.Now; 
    Value = (int)ClockInfoFromSystem.DayOfWeek; 

    var output = productsArray[Value2, Value]; 

    txtOutput.Text += "Products completed on that day are: " + output; 

} 
+0

你能解釋爲什麼_Value =(int)ClockInfoFromSystem.DayOfWeek; _不起作用嗎? – Steve

+0

@在之前將其轉換爲導致錯誤的轉換。因爲用戶輸入的東西是一個字符串命令,我試圖將字符串轉換爲一個int,這在「Monday」的情況下並不真正起作用。我試圖看看是否有替代方案。 – Anon

+0

重複說明如何在枚舉(DayOfWeek)中轉換字符串(您的用戶輸入)。你的代碼,以獲得整數,應該工作。我也應該向已經指向上述重複的用戶道歉。你是對的。 – Steve

回答

1

我建議寫一個方法,如dayToInt(String day)這是一個簡單的switch陳述或if...else if...else鏈實現。

+0

這個答案看起來像是對我來說正確的答案,轉換爲枚舉將是有風險和不可靠的(特別是如果,如上所述,需要其他語言) – Slicc

+0

是否有教程或MSDS資源?我似乎無法找到一個。 – Anon

+0

@Anon我懷疑是否有專門針對這個問題的教程。這是非常基本的C#。任何一般的C#教程或教科書都會解釋如何編寫方法以及如何使用switch和if ... else工作。 –

相關問題