リストがあります。入力した値がそのリスト内にある場合、結果が返されます。値が結果の範囲外であっても、mod%のために結果が返されます。私の問題を言葉で説明するのは本当に難しいので、コードを見てみましょう:
List<int> list1 = new List<int>(){ 0, 1, 2, 3, 4, 5, 6 };
int value = 3; // what's this number to us?
string result = "";
int starting_number = 3;
if (value == list1[(list1.IndexOf(starting_number) + 2) % list1.Count()])
{ result = "yeah"; }
else if (value == list1[(list1.IndexOf(starting_number) + 1) % list1.Count()])
{ result = "cool"; }
else if (value == list1[(list1.IndexOf(starting_number) + 0) % list1.Count()])
{ result = "one"; }
else if (value == list1[(list1.IndexOf(starting_number) - 1) % list1.Count()])
{ result = "noo"; }
else {result = "oops cant find it"; }
- start_number。この数は一定です。すべての測定は、この番号に関連して行われます。
- value-入力した値。この値の結果を取得する必要があります
- 結果-単なる文字列
それがどのように機能するかです:
start_number = 3、value = 2 => result = "noo"(2 = IndexOf(starting_number)-1であるため)
start_number = 6、value = 0 => result = "cool" (6 + 1 = 0)
start_number = 0、value =6=>エラーが発生します。(0-1 = 6)
このエラーを取り除くためにコードを改善する方法はありますか?基本的に、starting_numberの「後」にある値を取得できますが、starting_numberの「前」に値を取得することはできません。
(6 + 1 = 0)の場合:リストは次のようになります6,0,1,2,3 .. ..
(0 --1 = 6)の場合:リストは次のようになります... 4,5,6,0
ご不明な点がございましたら、お問い合わせください。言葉で問題を説明するのは本当に難しいですが、例がより明確になっていると思います。
start_numberより下の値をどのように取得しますか?