运营商||不能应用于类型bool和字符串/字符串和字符串 [英] Operator || cannot be applied to type bool and string / string and string

查看:199
本文介绍了运营商||不能应用于类型bool和字符串/字符串和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的简单任务非常困难。

Simple task that I am finding really difficult.

 Console.Write("[" + CurrentTime + "] Name a day of the week? ");
 string vDay = Console.ReadLine();
 if (vDay != "Monday" || "Tuesday" || "Wednesday" || "Thursday" || "Friday")
 {
  Console.WriteLine("that is not a valid day of the week");
 }

我使用时口渴!=它给我一个错误,说无法应用 bool和字符串没有!=只是=我得到字符串和字符串

Firsty when I use != it gives me an error saying "cannot be applied to bool and string" without the != and just the = I get "string and string"

基本上我想要做的是如果有人输入你好为例如,它会说这不是一周中的有效日期。

Basically what I am trying to do is if someone types "hello" for example it will say that is not a valid day of the week.

这么简单的任务,但我发现它很难,感谢您的帮助。

Such a simple task but im finding it so difficult, thanks for any help.

推荐答案

由于这里的所有答案都没有解释为什么你首先要面对这个问题,我将尝试这样做。

Since all the answers here do not explain why you are facing that problem in the first place, I shall attempt to do so.


当我使用时!=它给我一个错误,说不能应用于bool
和字符串

when I use != it gives me an error saying "cannot be applied to bool and string"

这是因为,在您的代码中:

This is because, in your code:

...vDay != "Monday" || "Tuesday" ...

vDay 是与成功评估的字符串Monday相比较,然后将结果(布尔值)与字符串Tuesday进行比较。这给你的问题是 ...不能应用于bool和字符串..

vDay is compared to string "Monday" which evaluates successfully and then the result (which is boolean) is compared to string "Tuesday". This gives you the problem of "...cannot be applied to bool and string.."


!=和只是=我得到字符串和字符串

without the != and just the = I get "string and string

vDay 被赋予星期一的值(这可能会导致字符串星期一)但是字符串星期一正在与字符串星期二进行比较。这会给您带来 ...无法应用的问题字符串和字符串..

vDay is being assigned the value of "Monday" (which could result in string "Monday") however the string "Monday" is being compared with string "Tuesday". This gives you the problem of "...cannot be applied to string and string..".

正确的方法是分别指定运算符:

The correct way would be to specify operators separately:

...vDay != "Monday" && vDay != "Tuesday"...

或使用其他答案中最佳描述的其他方式。

Or using the other ways as best described by other answers.

这篇关于运营商||不能应用于类型bool和字符串/字符串和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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