wxw7589 发表于 2008-8-21 18:21

新手问题:变量“i”已赋值,但其值从未使用过 谢谢

有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
using System;

namespace PermutationNS
{
    public static class Demo
    {
        public static void Main()
        {
            int i = 0;
            int j = 0;
            int k = 0;
            Permutation.permute();
        }

    }

    public static class Permutation
    {
        public static void permute()
        {
            for (i = 0; i <= 4; i++)
            {
                for (j = 0; j <= 4; j++)
                {
                    for (k = 0; k <= 4; k++)
                    {
                        if (i != j && j != k && k != i)
                        {
                            Console.WriteLine("组成的三位数为:{0}{1}{2}", i, j, k);
                        }
                    }
                }
            }
        }
        
    }
}

编译错误:

cs(9,17): warning CS0219: 变量“i”已赋值,但其值从未使用过
cs(10,17): warning CS0219: 变量“j”已赋值,但其值从未使用过
cs(11,17): warning CS0219: 变量“k”已赋值,但其值从未使用过
cs(21,18): error CS0103: 当前上下文中不存在名称“i”
cs(21,25): error CS0103: 当前上下文中不存在名称“i”
cs(21,33): error CS0103: 当前上下文中不存在名称“i”
cs(23,22): error CS0103: 当前上下文中不存在名称“j”
cs(23,29): error CS0103: 当前上下文中不存在名称“j”
cs(23,37): error CS0103: 当前上下文中不存在名称“j”
cs(25,26): error CS0103: 当前上下文中不存在名称“k”
cs(25,33): error CS0103: 当前上下文中不存在名称“k”
cs(25,41): error CS0103: 当前上下文中不存在名称“k”
cs(27,29): error CS0103: 当前上下文中不存在名称“i”
cs(27,34): error CS0103: 当前上下文中不存在名称“j”
cs(27,39): error CS0103: 当前上下文中不存在名称“j”
cs(27,44): error CS0103: 当前上下文中不存在名称“k”
cs(27,49): error CS0103: 当前上下文中不存在名称“k”
cs(27,54): error CS0103: 当前上下文中不存在名称“i”
cs(29,68): error CS0103: 当前上下文中不存在名称“i”
cs(29,71): error CS0103: 当前上下文中不存在名称“j”
cs(29,74): error CS0103: 当前上下文中不存在名称“k”

师妃暄 发表于 2008-8-21 18:29

这是肯定的.i出在两个不同的类中哦.
public static void permute()
        {
            for (int i = 0; i <= 4; i++)
            {
                for (int j = 0; j <= 4; j++)
                {
                    for (int k = 0; k <= 4; k++)
                    {
                        if (i != j && j != k && k != i)
                        {
                            Console.WriteLine("组成的三位数为:{0}{1}{2}", i, j, k);
                        }
                    }
                }
            }

wxw7589 发表于 2008-8-21 18:36

回复 2# 师妃暄 的帖子

先谢谢呵呵!我去掉了一个类,还是会出现同样的编译结果,为什么啊?

wxw7589 发表于 2008-8-21 18:41

以解决谢谢o(∩_∩)o...哈哈

using System;

namespace PermutationNS
{
    public static class Permutation
    {
        public static void Main()
        {
            int i = 0;
            int j = 0;
            int k = 0;
            for (i = 0; i <= 4; i++)
            {
                for (j = 0; j <= 4; j++)
                {
                    for (k = 0; k <= 4; k++)
                    {
                        if (i != j && j != k && k != i)
                        {
                            Console.WriteLine("组成的三位数为:{0}{1}{2}", i, j, k);
                        }
                    }
                }
            }
        }        
    }
}

页: [1]

编程论坛