本文介绍了如果(几个)替代品之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一些代码需要知道int变量是否是几个 可能值中的一个。 什么是更好的方法来执行测试比堆叠一堆案例: 在switch语句中?我也不想在 和if语句中有一堆逻辑OR(例如,if(someInt == 25 || someInt == 18 || someInt == 87 ....) 有什么建议吗? 谢谢! I have some code that needs to know if an int variable is one of severalpossible values. What is a better way of performing the test than stacking a bunch of case:in a switch statement? I also don''t want to have a bunch of logical ORs inan if() statement (e.g., if (someInt == 25 || someInt == 18 || someInt ==87....) Any suggestions? Thanks!推荐答案 我喜欢开关中​​堆叠的盒子。有很多创意方法 你可以做到这一点。这是一个,只是为了好玩: int [] vals = {25,18,87}; int someInt = 25; bool found = false; if(((System.Collections.IList)vals).Contains(someInt)) found = true; - Tom Porterfield I like the stacked case in a switch. There are lots of creative waysyou could do this. Here''s one, just for fun: int [] vals = {25,18,87};int someInt = 25;bool found = false;if (((System.Collections.IList)vals).Contains(someInt ))found = true;--Tom Porterfield 为什么不呢? if ... then ... else if else,else if ... sequen ce,这些 是C#语言为你提供的技巧。 在你梦想的编程语言中,语法是什么样的? 好​​奇, Tom Dacon Dacon软件咨询 Why not? Short of an if ... then ... else if ... else if... sequence, theseare the techniques that the C# language syntax offers you. In the programming language of your dreams, what would the syntax look like? Just curious,Tom DaconDacon Software Consulting 这篇关于如果(几个)替代品之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 05:57