如何检查一个列表框中的几个选定元素是否可以被 3 整除并且它们是整数?
我有一个包含 5 个元素的列表框: 3 猫 狗 4 9
现在我必须检查所选元素是否是整数以及它们是否可以被3整除,如果是我应该对它们求和。谢谢 (例如,选择了 3、狗和 9,我应该将 12 添加到标签中)
I have one listbox with 5 elements:
3
cat
dog
4
9
Now I have to check if the selected elements are integers and if they are divisible by 3, if they are I should sum them.thanks
(where for example, 3, dog and 9 are selected and I should get 12 into a label)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
迭代每个元素并使用
int.TryParse
。如果可以解析,请检查intvalue % 3 == 0
以查看它是否可以被 3 整除。Iterate over each element and use
int.TryParse
. If it parses, checkintvalue % 3 == 0
to see if it's divisible by three.假设您的列表框中的项目位于字符串列表中,这将是解决该问题的 Linq 方法:
Suppose you have the items from your listbox in a list of strings this would be a Linq approach to the problem: