比较两个相同的字符数组
using System;namespace Compare_array
{
class Compare_array
{
static void Main(string[] args)
{
int i, flag = 1;
char[] str1 = new char[3];
char[] str2 = new char[3];
Console.WriteLine("Enter the first string of length five");
str1 = Console.ReadLine().ToCharArray();
Console.WriteLine("Enter the second string of length five");
str2 = Console.ReadLine().ToCharArray();
for (i = 0; i <= 2; i++)
{
if (str1[i] != str2[i])
{
flag = 0;
break;
}
if (flag == 1)
{
Console.WriteLine("The strings are identical");
}
else
{
Console.WriteLine("The strings are not identical");
}
}
}
}
}
输入的str1为 a b c; str2为 a c b;那为什么会输出两次 The strings are identical b 与 c 不是不相同的吗?那么应该就跳出循环,只输出一次The strings are identical 还请前辈指点