无法将类型“int”隐式转换为“bool”

曼先生
using System;

namespace Codes
{
    class Program
    {
        static void Main(string[] args)
        {
            String name = RandomNames();
            Console.Write(name);
            Console.ReadKey();
        }

        public static string RandomNames()
        {
            Random numGen = new Random();
            string vowel = "aeiou";
            string conso = "bcdfghjklmnpqrstvwxyz";
            int nameLen = numGen.Next(4,10);

            string result = "";
            for (int i = 0; i < nameLen; i++) 
            {
                if (i % 2) 
                {
                    result += vowel[numGen.Next(0, 4)]; // 4 is final index of vowel.
                } 
                else 
                {
                    result += conso[numGen.Next(0, 20)]; // 20 is final index of consonants.
                }
            }
            return result;
        }
    }

它说 if 语句中的“i”不能隐式地将类型“int”转换为“bool”,我不知道有什么问题,因为它已经是一个 int for 循环

安德烈·许威

i % 2 是 int 类型,而不是 bool 类型,C# 无法自动进行转换,因此只需尝试:

i % 2 != 0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将类型'int'隐式转换为'bool'Unity

无法将类型“bool”隐式转换为“int”

“无法将类型'bool []'隐式转换为'object []'”

无法将类型'bool'隐式转换为'float'

无法将类型'ulong'隐式转换为'bool'

Unity无法将类型“ string”隐式转换为“ bool”

无法将类型'string'隐式转换为'bool'[If语句]

无法将类型 'IEnumerable<int> 隐式转换为 'int'

无法将类型int []隐式转换为int?[]

无法将类型'double'隐式转换为'int'。-错误

无法将类型“ double”隐式转换为“ int”

无法将类型'float'隐式转换为'int'

无法将类型隐式转换为int []

无法将类型 *** 隐式转换为 ****

在条件中使用按位运算符-无法将类型'int'隐式转换为'bool'

c# - 如何修复停止“无法将int类型隐式转换为bool”错误代码

如果语句不能隐式将类型转换为bool?

无法将类型'void'隐式转换为'System.Collections.Generic.Dictionary <string,bool>

无法将类型“bool”隐式转换为“Microsoft.AspNetCore.Mvc.ActionResult”

无法将类型'bool'隐式转换为'System.Threading.Tasks.Task'

Unity3d“无法将类型“ void”隐式转换为“ bool””

实体框架错误无法隐式将类型“bool”转换为“string”

如何解决错误:无法将类型“long”隐式转换为“bool”?

我的代码返回无法将 int 隐式转换为 bool

当似乎不涉及“ long”类型时,无法将“ long”类型隐式转换为“ int”

数组出现问题(无法将类型'int'隐式转换为'int []')

C# - 无法将类型“int”隐式转换为“int[][]” - CodeFights

webservice无法构建-“无法将类型'string'隐式转换为'int'”

无法将int隐式转换为int []