以下代码不知道错在哪里,我是菜鸟。有一数组arr["aa","bb","cc"],用foreach 来检查 dd 在不在arr中,如果在就显示“此字符在arr中”,如果不在就显示“此字符不在arr中”,不知道错在哪里??各位大侠帮帮忙啊。using System;
using System.Collections.Generic;
using System.Text;namespace Hello
{
class Program
{
static void Main(string[] args)
{
string[] arr ={ "aa", "bb", "cc" };
foreach (string dd in arr)
{
Console.WriteLine("此字符在在arr中");
}
Console.WriteLine("此字符不在arr中");
}
}
}
using System.Collections.Generic;
using System.Text;namespace Hello
{
class Program
{
static void Main(string[] args)
{
string[] arr ={ "aa", "bb", "cc" };
foreach (string dd in arr)
{
Console.WriteLine("此字符在在arr中");
}
Console.WriteLine("此字符不在arr中");
}
}
}
{
if ("dd" == dd)
{
Console.WriteLine("此字符在在arr中");
}
}
Console.WriteLine("此字符不在arr中");
foreach (string dd in arr)
{
if(dd=="dd")
{
Console.WriteLine("字符串dd在arr中");
return;
}
}
Console.WriteLine("字符串dd不在arr中");
using System.Collections.Generic;
using System.Text; namespace Hello
{
class Program
{
static void Main(string[] args)
{
string[] arr ={ "aa", "bb", "cc" };
bool found = false;
foreach (string dd in arr)
{
if (dd == "dd")
{
found = true;
break;
}
}
Console.WriteLine("此字符{0}在arr中", found ? "" : "不");
}
}
}
严重同意:using System;
using System.Collections.Generic; namespace Hello
{
class Program
{
static void Main()
{
string[] arr ={ "aa", "bb", "cc", "dd" };
Console.WriteLine("此字符{0}在arr中", new List<string>(arr).Contains("dd") ? "" : "不");
}
}
}
using System.Collections.Generic;
using System.Text;namespace Hello
{
class Program
{
static void Main(string[] args)
{
string[] arr ={ "aa", "bb", "cc" };
string s = "bb";
foreach (string s in arr)
if (s == "dd")
{
Console.WriteLine("此字符在在arr中");
}
Console.WriteLine("此字符不在arr中");
}
}
}
namespace Hello
{
class Program
{
static void Main(string[] args)
{
string[] arr ={ "aa", "bb", "cc" };
foreach (string dd in arr)
{
Console.WriteLine("此字符在在arr中");
}
Console.WriteLine("此字符不在arr中");
}
}
}
foreach (string dd in arr)
这一句楼主没有理解
这个dd是一个变量的名字,
{
Console.WriteLine("此字符在在arr中");
}
下面这一段根本没有进行比较的过程!
知道错哪里了吧!
就好比你
string x;
但是你没有拿这个x做事情
应改为:
string[] arr ={ "aa", "bb", "cc" };
string s = "bb";
foreach (string str in arr) //不能与上已声明的变量同名
if (str ==s )
{
Console.WriteLine("此字符在在arr中");
return;
}
Console.WriteLine("此字符不在arr中");
应改为:
string[] arr ={ "aa", "bb", "cc" };
string s = "bb";
foreach (string str in arr) //不能与上已声明的变量同名
if (str ==s )
{
Console.WriteLine("此字符在在arr中");
return;
}
Console.WriteLine("此字符不在arr中");
using System.Collections.Generic;
using System.Text;namespace Hello
{
class Program
{
static void Main(string[] args)
{
string[] arr ={ "aa", "bb", "cc" };
string s = "bb";
foreach (string s in arr)
if (s == "dd")
{
Console.WriteLine("此字符在在arr中");
}
Console.WriteLine("此字符不在arr中");
}
}
}
using System.Collections.Generic; namespace Hello
{
class Program
{
static void Main()
{
string[] arr ={ "aa", "bb", "cc" };
string s = "bb"; Console.WriteLine("此字符{0}在arr中", new List<string>(arr).Contains(s) ? "" : "不");
}
}
}
using System.Collections.Generic;
using System.Text; namespace Hello
{
class Program
{
static void Main(string[] args)
{
string[] arr ={ "aa", "bb", "cc" };
string s1 = "bb";
foreach (string s in arr)
if (s == s1)
{
Console.WriteLine("此字符在在arr中");
return;
}
Console.WriteLine("此字符不在arr中");
}
}
}
{
Console.WriteLine("此字符{0}在arr中",dd);
}
string[] arr ={ "aa", "bb", "cc" };
string s1 = "bb";
foreach (string s in arr)
if (s == s1)
{
Console.WriteLine("此字符在在arr中");
return;
}
Console.WriteLine("此字符不在arr中");
s1 是用来保存你要查找的字符串 "bb" 的。
foreach (string dd in arr)
{
Console.WriteLine("此字符{0}在arr中",dd);
} 这其中的第2行与下面相同:
for(int i=0;i<arr.length;i++)for循环三次,其中的dd分别代表arr[0]、arr[1]、arr[2],
Console.WriteLine共打印了三行:
此字符aa在arr中
此字符bb在arr中
此字符cc在arr中
如果要判断是否在数组中的话,可如3楼所列,用下列程序:
string[] arr ={ "aa", "bb", "cc" };
bool found = false;
foreach (string dd in arr)
{
if (dd == "dd")
{
found = true;
break;
}
}
Console.WriteLine("此字符{0}在arr中", found ? "" : "不");
前两个就已经够了,搞清楚foreach的功能吧