注册 登录
编程论坛 C# 论坛

想在一个for中写入两个约束条件,用“与”老显示错误,不知道咋整,求解

米粒大小3 发布于 2016-11-02 21:29, 2488 次点击
写成下面形式时,会依次循环下去,我想要m和i一起变化,就是m=0时,i=0;m=1时,i=1;m=2时,i=2;然后j都是0,1,2,3的循环下去。想用与来控制下,但老是错误,不知道为什么?
 for (int m = 0; m < 3; m++)
                for (int i = 0; i < 3;i++ )
                {
                    for (int j = 0; j < 4; j++)
                    {                       
                        x[m, 1] = x[m, 0] + s[i, j] * System.Math.Pow(2, 3 - j);
                    }
                }
5 回复
#2
yhlvht2016-11-02 21:36
for (int i = m; i < 3; i++)
#3
米粒大小32016-11-03 08:16
回复 2楼 yhlvht
没敢这样试
#4
米粒大小32016-11-03 08:24
回复 3楼 米粒大小3
还是会出现错误,不知道错哪了,你帮我看看呗,我是想将一个0,1编码的数组的每一行转换成十进制
 double[,] s = { { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 1, 1, 1, 0 } };
            double[,] x = new double[3, 1];
            for (int m= 0 ; m < 3; m++)
               for (int i =m ; i < 3;i++ )
                {
                    for (int j = 0; j < 4; j++)
                    {                       
                        x[m, 0] = x[m, 0] + s[i, j] * System.Math.Pow(2, 3 - j);
                    }
                }
            Console.WriteLine("{0}/n{1}/n{2}", x[0, 0], x[0, 1], x[0, 2]);
            Console.ReadKey();     
#5
yhlvht2016-11-03 20:56
程序代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace array
{
    class Program
    {
        static void Main(string[] args)
        {
            double[,] s = { { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 1, 1, 1, 0 } };
            List<int> list = new List<int>();
            for (int i = 0; i < s.GetLength(0); i++)
            {
                string str = string.Empty;
                for (int k = 0; k < s.GetLength(1); k++)
                {
                    str += s[i, k].ToString();
                }
                list.Add(Convert.ToInt32(str, 2));
            }
            foreach (int num in list)
            {
                Console.WriteLine(num);
            }
            Console.ReadKey();
        }
    }
}
#6
米粒大小32016-11-04 10:53
回复 5楼 yhlvht
明白了,非常感谢!!!但我又碰着个问题了,还是数组S,现在我想要第二行的前两个元素,有什么好的方法么?
1