【问题标题】:How do i read a boolean value?我如何读取布尔值?
【发布时间】:2021-09-14 02:12:51
【问题描述】:

我正在制作我想要制作的应用程序的基础,但是在这个“原型”中我如何选择用户可能想要与之交谈的朋友存在问题(我猜它是一个原型?)

string friend;
string friend2;
string friend3;
bool option;

Console.WriteLine("Who would you like to talk to first? " + friend + ", " + friend2 + ", " + friend3);

option = (Console.ReadLine);

if(option = friend)
{

}

这就是问题的代码。我应该删除它还是有人可以帮忙?

【问题讨论】:

  • 我认为您应该从语法等 c# 基础知识开始。
  • if(option == friend) 问题是使用等号时,一个表示赋值,两个表示相等性测试。
  • 布尔到字符串的比较是错误的。

标签: c# boolean


【解决方案1】:

我觉得你有点困惑,你使用的是赋值运算符而不是比较运算符。

这里是一个例子

int a = 1 + 2 + 3;
int b = 6;
Console.WriteLine(a == b);  // output: True

char c1 = 'a';
char c2 = 'A';
Console.WriteLine(c1 == c2);  // output: False
Console.WriteLine(c1 == char.ToLower(c2));  // output: True

你也可以阅读这个https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators

【讨论】:

  • 虽然 OP 当然 应该知道你在写什么,但你的答案实际上并没有 回答 OP 发布的问题。请阅读how to write a good answer
【解决方案2】:

option 变量在您的情况下应该是 string 类型。 然后,您需要更改此位(赋值运算符)

if(option = friend)

到这个(比较运算符)

if(option == friend)

所以最后你会得到(在 readline 命令修复之后)

string friend;
string friend2;
string friend3;
string option;

Console.WriteLine("Who would you like to talk to first? " + friend + ", " + friend2 + ", " + friend3);

option = Console.ReadLine();

if(option == friend)
{

}

玩得开心!

【讨论】:

    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2014-10-19
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多