| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 467 人关注过本帖
标题:数组的问题,哪儿出问题了,该怎么弄?
只看楼主 加入收藏
吕游
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-5-10
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:4 
数组的问题,哪儿出问题了,该怎么弄?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace weizhi
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,]s1 = new int[,]{{45,23},{1,2},{8,9}};
            int[,]s2 = new int[,]{{ 4,23},{10,20},{7,9}};
            int i,j;
            string output="";
            foreach (int a in s1)
            {
                for(i=0;i<4;++i)
                    for (j = 0; j < 2; ++j)
                    {
                        if (a == s2[i,j])
                            goto done;
                    }
                done: output += string.Format("找到{0}位置[{1}][{2}]")+"\n";
                output+=string.Format("没有找到{0}位置[{1}][{2}]");
                MessageBox.Show(output);



                    
            

 
 
            }
        }
    }
}
2010-09-28 21:31
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:10 
首先,你的第一个for循环没有加上大括号,程序就在自己那里循环;其次你的output位置没有给出变量是什么,程序不知道你要输出什么东西!!
2010-09-28 22:28
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:0 
忘了改了的了,下面是改过的,但是我不知道你具体要干什么,也不知道你要输出什么,这里只是把程序改成编译可以通过的状态......
int[,]s1 = new int[,]{{45,23},{1,2},{8,9}};
            int[,]s2 = new int[,]{{ 4,23},{10,20},{7,9}};
            int i=0,j=0;
            string output="";
            foreach (int a in s1)
            {
                for (i = 0; i < 3; ++i)
                {
                    for (j = 0; j < 2; j++)
                    {
                        if (a == s2[i, j])
                            goto done;
                    }
                }
                done:
                output += string.Format("找到{0}位置[{1}][{2}]","s2",i,j) + "\n";
                output += string.Format("没有找到{0}位置[{1}][{2}]",i,j,a);
                MessageBox.Show(output);
            }
2010-09-28 22:30
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:10 
具体细节自己去调试便知:
情况1:-->找到后跳转,不会再次执行(但实际上有2个相等的值)
程序代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ConsoleTest
{
    class Program
    {
       static  StringBuilder output = new StringBuilder();
        static void Main(string[] args)
        {
            StringBuilder output = new StringBuilder();
            int i = 0, j = 0;
            int temp = 0;
            int[,] s1 = new int[,] { { 45, 23 }, { 1, 2 }, { 8, 9 } };
            int[,] s2 = new int[,] { { 4, 23 }, { 10, 20 }, { 7, 9 } };
           
         
            foreach (int a in s1)
            {          
                i = 0;
                for (; i < 3; i++)
                {
                    j = 0;
                    for (; j < 2; j++)
                    {                     
                        if (a == s2[i, j])
                        {
                            temp = a;
                            goto done;
                        }
                        else continue;
                    }
                }           
            }
            done:
            output.Append(string.Format("找到{0}位置[{1}][{2}]", temp, i, j) + "\n");
            //output += string.Format("没有找到{0}位置[{1}][{2}]", a, i, j);
            MessageBox.Show(output.ToString());
           
        }
    }
}
完善第一种情况: -->输出了2个相等的值

程序代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ConsoleTest
{
    class Program
    {
       static  StringBuilder output = new StringBuilder();
       static int i =0, j =0 ;
       static int temp=0;
        static void Main(string[] args)
        {
            int[,] s1 = new int[,] { { 45, 23 }, { 1, 2 }, { 8, 9 } };
            int[,] s2 = new int[,] { { 4, 23 }, { 10, 20 }, { 7, 9 } };
           
         
            foreach (int a in s1)
            {          
                i = 0;
                for (; i < 3; i++)
                {
                    j = 0;
                    for (; j < 2; j++)
                    {                     
                        if (a == s2[i, j])
                        {
                            temp = a;
                            show();
                        }
                        else continue;
                    }
                }           
            }      
           
        }
        static void show() {
            output.Append(string.Format("找到{0}位置[{1}][{2}]", temp, i, j) + "\n");
            //output += string.Format("没有找到{0}位置[{1}][{2}]", a, i, j);
            MessageBox.Show(output.ToString());
        }
    }
}


C#超级群 74862681,欢迎大家的到来!
2010-09-28 23:09
吕游
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-5-10
收藏
得分:0 
谢谢!
2010-09-29 17:51
快速回复:数组的问题,哪儿出问题了,该怎么弄?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021865 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved