我怎样才能多个变量比较单一的状况? [英] How can I compare multiple variables to a single condition?

查看:146
本文介绍了我怎样才能多个变量比较单一的状况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的:

if (input.Text.ToUpper() == "STOP")

但有这么多,我就不能单独指定所有这样的可能值:

But there are so many possible values that I wouldn't be able to specify them all separately like this:

if (input.Text.ToUpper() == "STOP" || input.Text.ToUpper() == "END")

有没有一种方式,你可以做这样的事情:

Is there a way that you can do something like this:

if (input.Text.ToUpper() == "STOP", "END", "NO", "YES")

因此使用STOP,END,NO或YES会做任务?

So that using STOP, END, NO, or YES will do the task?

使用任何含有将无法正常工作,其他时间接受了的话会在他们的字STOP和END。

Using any contains won't work, other times accepted words will have the word STOP and END in them.

推荐答案

使用为此目的数组:

     string[] stopsWords = new string[] { "stop", "break", .... };
     for (int i = 0; i < stopWords.Length; i++) if (textBox.Text.ToLower() == stopWords[i])
     {
          // do what ever
     } 

这篇关于我怎样才能多个变量比较单一的状况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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