我正在嘗試用 C# 編寫一些代碼,它允許我在提供以下輸入后訪問下表中的值:地面(巖石、硬土、軟土)矩震級 (6.5, 7.5, 8.5)source_to_source (0-20, 20-50, 50-100)我已嘗試使用以下代碼,但不斷收到消息:System.Collections.Generic.KeyNotFoundException 發生 - '字典中不存在給定的鍵'。有人可以幫我讓它工作嗎?有沒有更有效的方式來編寫這段代碼?using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _20180607_dict_example1{ class Program { static void Main(string[] args) { string ground = "Rock"; string moment_magnitude = "6.5"; string source_to_source = "0-20"; double ratio_peak; int first_value; int second_value; int third_value; // 0b. Calculate ratio peak from Table 2 in Hashash paper var valueDict = new Dictionary<string, int> { { "6.5", 0 }, { "7.5", 1 }, { "8.5", 2 }, { "rock", 0 }, { "stiff soil", 1 }, { "soft soil", 2 }, }; if (valueDict.ContainsKey(moment_magnitude)) { first_value = valueDict[moment_magnitude]; Console.WriteLine(first_value); } if (valueDict.ContainsKey(ground)) { second_value = valueDict[ground]; Console.WriteLine(second_value); } int[,] array = new int[3, 3] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } }; Console.WriteLine(array[valueDict[ground], valueDict[moment_magnitude]]); var valueDict_source_to_source = new Dictionary<string, int> { { "0-20", 0 }, { "20-50", 1 }, { "50-100", 2 } }; if (valueDict_source_to_source.ContainsKey(source_to_source)) { third_value = valueDict_source_to_source[source_to_source]; Console.WriteLine(third_value); }
- 2 回答
- 0 關注
- 205 瀏覽
添加回答
舉報
0/150
提交
取消