如果(几个)替代品之一 [英] if (one of several) alternatives

查看:85
本文介绍了如果(几个)替代品之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码需要知道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 several
possible 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 in
an if() statement (e.g., if (someInt == 25 || someInt == 18 || someInt ==
87....)

Any suggestions?

Thanks!

推荐答案

你好!


怎么样


switch( someInt)

{

案例25:

案例18:

案例87:

...

案例129:

{

...在这里工作

休息; < br $>
}

}


使可读性更高,我认为性能更好

更好,但也许有人可以对此有所了解?实际上,我认为

我会尝试将IL代码与if语句进行比较..

-

venlig hilsen /关心

anders borum

-
Hello!

What about:

switch (someInt)
{
case 25:
case 18:
case 87:
...
case 129:
{
... do work here
break;
}
}

Makes for a much higher readability, and I would think the performance is
better but perhaps someone could cast some light on this? Actually, I think
I''ll try to compare the IL code to an "if" statement ..

--
venlig hilsen / with regards
anders borum
--


RC写道:
我有一些需要知道int变量是否是几个可能值中的一个的代码。

执行测试比堆叠大量案例更好的方法是:
转换声明?我也不想在一个if()语句中有一堆逻辑OR(例如,if(someInt == 25 || someInt == 18 || someInt ==
87)。 ......)

有什么建议吗?
I have some code that needs to know if an int variable is one of several
possible 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 in
an if() statement (e.g., if (someInt == 25 || someInt == 18 || someInt ==
87....)

Any suggestions?




我喜欢开关中​​堆叠的盒子。有很多创意方法

你可以做到这一点。这是一个,只是为了好玩:


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 ways
you 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


>什么是更好的方式执行测试而不是堆叠一堆case:
> What is a better way of performing the test than stacking a bunch of case:
在switch语句中?我也不想在if()语句中有一堆逻辑OR(例如,if(someInt == 25 || someInt == 18 || someInt ==
87 ....)
in a switch statement? I also don''t want to have a bunch of logical ORs in
an if() statement (e.g., if (someInt == 25 || someInt == 18 || someInt ==
87....)




为什么不呢? 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, these
are 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 Dacon
Dacon Software Consulting


这篇关于如果(几个)替代品之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆