我正在尝试用 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 关注
- 186 浏览
添加回答
举报
0/150
提交
取消