gpt4 book ai didi

c# - 错误1无法将类型'char'隐式转换为'bool'

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:26 25 4
gpt4 key购买 nike

我看过其他问题,但是答案似乎太复杂了,无法理解,所以这是我的代码:

注意:我是C Shar语言的新手

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Input_Program
{
class Program
{
private static void Main()
{
Console.WriteLine("Welcome to my bool program!");
Console.WriteLine("Input a NON capital y or n when told to.");


char Y;
char N;

if(Y = 'y')
{}
}
}
}


谢谢
谢谢

最佳答案

您缺少等号:if(Y == 'y')

现在,将值'y'分配给Y,这也是赋值表达式的结果。这是一个char,而编译器期望的是bool结果。

更新:

当然,如thephpdeveloper所说,编译器还将坚持要求您在读取Y之前为其分配一个值。所以:

char Y = 'y';
char N = 'n';

if(Y == 'y')
{}

关于c# - 错误1无法将类型'char'隐式转换为'bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5399972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com