use*_*114 2 c# arrays string char
我有一个字符串数组,如下所示.我正在迭代字符串数组并打印每个字符串元素的第四个字符,它始终为1.
我一直在尝试做的是实现一个if语句,这样如果每个元素的每个第四个字符为1然后打印一些东西,但我不确定如何访问我正在迭代的每个元素.
我添加了一个带有一些伪代码的if语句来尝试突出我想要做的事情.谢谢你的帮助.
namespace ConsoleApplication6
{
class Program
{
static void Main()
{
string[] bitstrings = new string[16];
bitstrings[0] = "00101";
bitstrings[1] = "00001";
bitstrings[2] = "00001";
bitstrings[3] = "00101";
bitstrings[4] = "00101";
bitstrings[5] = "00101";
bitstrings[6] = "00101";
bitstrings[7] = "00101";
bitstrings[8] = "00101";
bitstrings[9] = "00101";
bitstrings[10] = "00101";
bitstrings[11] = "00101";
bitstrings[12] = "00101";
bitstrings[13] = "00101";
bitstrings[14] = "00101";
bitstrings[15] = "00101";
for (int x = 0; x < bitstrings.Length; x++)
{
int s = bitstrings[x][4];
Console.ReadLine();
}
if(/*all bitstrings[x][4] = 1*/)
{
//print something
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的代码就在那里,你只需要记住字符串中的每个字符,无论其值如何,仍然是一个字符.你的if语句也需要在for循环中.
for (int x = 0; x < bitstrings.Length; x++)
{
//3 instead of 4 because arrays are 0 indexed
if(bitstrings[x][3] == '1')
{
//valid char
}
}
//Also don't need to read like every iteration.
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
编辑:如果所有第4个元素必须为1,则将其修改为包含布尔值,如果找到无效的char,则将其分解
bool allOne = true;
for (int x = 0; x < bitstrings.Length; x++)
{
//3 instead of 4 because arrays are 0 indexed
if(bitstrings[x][3] != '1')
{
allOne = false;
break;
}
}
if(allOne)
{
//Do something
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
757 次 |
| 最近记录: |