c# - 无法将类型“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”,我不知道这有什么问题,因为它已经是 for 循环中的 int
解决方案
i % 2
是 int 类型,而不是 bool,C# 无法自动进行转换,所以只需尝试:
i % 2 != 0
推荐阅读
- docker - Gramex 在 Docker 容器中引发“inotify watch limited reached”错误
- css - Vue/Vuetify 转换只有一种方式
- flutter - 首次打开时,小部件树中的颤振功能单个容器
- linear-programming - 混合整数线性规划中的整数除法
- html - 如何让一个单元格高于另一个单元格
- javascript - 如何使用 H.ui.MapSettingsControl 选择 POI 地图?
- javascript - Vue 中未定义的 $auth 使用 @websanova/vue-auth
- react-admin - 如何在 react-admin 中重用过滤器输入
- c++ - 使用 QString 打印带有加号或减号的 double
- tsql - 存储在 SQL 中的十六进制数据