以下代码不知道错在哪里,我是菜鸟。有一数组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中");
            
        }
    }
}

解决方案 »

  1.   

    string[] arr = { "aa", "bb", "cc" };            foreach (string dd in arr)
                {
                    if ("dd" == dd)
                    {
                        Console.WriteLine("此字符在在arr中");
                    }
                }
                Console.WriteLine("此字符不在arr中");
      

  2.   

    string[] arr ={ "aa", "bb", "cc" }; 
                
    foreach (string dd in arr) 

     if(dd=="dd")
     {
      Console.WriteLine("字符串dd在arr中");
      return;
     } 

    Console.WriteLine("字符串dd不在arr中"); 
      

  3.   

    using System; 
    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 ? "" : "不"); 
                
            } 
        } 
    }
      

  4.   

    为什么要用IF呢,foreach本身不就是有判断功能的吗?
      

  5.   

    印象中直接 arr.contains("dd");
      

  6.   


    严重同意: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") ? "" : "不"); 
                
            } 
        } 
    }
      

  7.   

    那你自己去查查foreach的作用,看看是不是有判断的功能。
      

  8.   

    首先谢谢各位大侠的指教,如果要检查的是一个变量,如下,这个变量S错在什么地方呢?using System;
    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中");
                
            }
        }
    }
      

  9.   

    帮楼主分析下错误
    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做事情
      

  10.   

    重复声明了字符串变量s
    应改为:
                string[] arr ={ "aa", "bb", "cc" }; 
                
                    string s = "bb"; 
                    foreach (string str in arr) //不能与上已声明的变量同名
                        if (str ==s ) 
                        { 
                            Console.WriteLine("此字符在在arr中"); 
                            return;
                        } 
                    Console.WriteLine("此字符不在arr中"); 
      

  11.   

    重复声明了字符串变量s
    应改为:
                string[] arr ={ "aa", "bb", "cc" }; 
                
                    string s = "bb"; 
                    foreach (string str in arr) //不能与上已声明的变量同名
                        if (str ==s ) 
                        { 
                            Console.WriteLine("此字符在在arr中"); 
                            return;
                        } 
                    Console.WriteLine("此字符不在arr中"); 
      

  12.   

    复制错了,是以下这段才对。using System;
    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中");
                
            }
        }
    }
      

  13.   

    using System; 
    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) ? "" : "不"); 
                
            } 
        } 
    }
      

  14.   

    using System; 
    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中"); 
                
            } 
        } 
    }
      

  15.   

    14楼的大侠,我是想理解清楚foreach的用法。
      

  16.   

    14楼的大侠,我是想理解清楚foreach的用法。
      

  17.   

    14楼的大侠,我是想理解清楚foreach的用法。
      

  18.   

    string[] arr = { "aa", "bb", "cc" };             foreach (string dd in arr) 
                { 
                    Console.WriteLine("此字符{0}在arr中",dd); 
                   
                } 
               
      

  19.   

    空军前辈,请问:程序是先做s==sl,再foreach?要不foreach s in arr 中S还没有定义?
    string[] arr ={ "aa", "bb", "cc" }; 
                
                    string s1 = "bb"; 
                    foreach (string s in arr) 
                        if (s == s1)
                        { 
                            Console.WriteLine("此字符在在arr中");
                            return; 
                        } 
                    Console.WriteLine("此字符不在arr中"); 
                
      

  20.   

    要用 if 做判断,foreach 只有遍历功能,没有判断功能。
      

  21.   


    s1 是用来保存你要查找的字符串 "bb" 的。
      

  22.   

    string[] arr = { "aa", "bb", "cc" }; 
    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 ? "" : "不"); 
      

  23.   

    简单问题,大家没必要回这么多相同的帖子嘛
    前两个就已经够了,搞清楚foreach的功能吧