Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 递增t2变量_C# - Fatal编程技术网

C# 递增t2变量

C# 递增t2变量,c#,C#,我有两个变量x和y,它们必须增加1,两个变量从1开始,第一个x必须增加1,直到它达到25,y保持在1。一旦达到25,x必须返回到1,y增加到y=2,重复(一旦x再次达到25,y将增加1)。这里是我的实现,但不起作用 using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { int x;

我有两个变量x和y,它们必须增加1,两个变量从1开始,第一个x必须增加1,直到它达到25,y保持在1。一旦达到25,x必须返回到1,y增加到y=2,重复(一旦x再次达到25,y将增加1)。这里是我的实现,但不起作用

using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int x;
            int y = 1;

            for (x = 1; x < 26; x++)
            {
                if (x == 25)
                {
                    x = 1;

                    for (y = 1; y < 30; y++)
                    {
                        Console.WriteLine("X = " + x + ", Y = " + y);
                    }
                }
            }
        }
    }
}
使用系统;
名称空间控制台AP3
{
班级计划
{
静态void Main(字符串[]参数)
{
int x;
int y=1;
对于(x=1;x<26;x++)
{
如果(x==25)
{
x=1;
对于(y=1;y<30;y++)
{
Console.WriteLine(“X=“+X+”,Y=“+Y”);
}
}
}
}
}
}
使用系统;
公共课程
{
公共静态void Main()
{

for(var y=1;y在for循环中操纵x不是标准做法。我假设您只希望两个变量都变为25(编辑:y现在变为30,就像在OP中一样)。所以技巧是将y放在外部,x放在内部

在这种情况下,仍然使用for循环而不是while循环是最简单的

试着这样做:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            for(int y = 0; y < 30; y++) //y goes up 1 every time x goes up 25
            {
                for(int x = 0; x < 25; x++) //counts x to 25
                {
                    Console.WriteLine("X=" + x + " Y=" + y);
                }
            }
        }
    }
}

使用系统;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
对于(int y=0;y<30;y++)//y每次x上升25,y上升1
{
对于(int x=0;x<25;x++)//计数x到25
{
Console.WriteLine(“X=“+X+”Y=“+Y”);
}
}
}
}
}

您也可以使用一点递归性来实现这一点(只需添加此项以获取更多信息)


把它写在纸上。让这里的人给你写会违背练习的目的。我建议在“for x”中“重置”x循环不是我们通常看到的东西。不要操纵循环的索引。它会产生比预期更多的错误。循环增加得越慢,应该在外部。循环增加得越快,应该在内部。您的Y循环是“在Y增加时打印当前X和Y”你的期望与此相反。这只是简单的循环,看看这是否接近你想要的WriteLine在while之外,所以它将只运行一次我将它放在外面以显示最终值。我现在更正了它有什么问题?x=26有问题吗?@Enigmativity。我欢迎反馈和反对票…@ChetanRanpariya数字30从何而来?还有,当你有
for
循环时,为什么要用whiles加增量呢?@seesharper 30被OP用来限制y的值。我只是试图遵循与OP相同的逻辑。是的。因为循环存在,但它们也需要增量…我用
for
循环…感谢您的反馈。在OP的问题中,他是
y<30
。我没有否决它,但这是一种非常复杂的方法,可以通过两个简单的嵌套循环来完成-至少可以传递循环大小,而不是调用函数run25…@seesharper,如我所说(添加此项以获取更多信息),我也不会使用它,只会使用2个“嵌套”循环。
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            for(int y = 0; y < 30; y++) //y goes up 1 every time x goes up 25
            {
                for(int x = 0; x < 25; x++) //counts x to 25
                {
                    Console.WriteLine("X=" + x + " Y=" + y);
                }
            }
        }
    }
}

public static void run25(ref int x, ref int y)
{
    if(x <= 25)
    { 
        Console.WriteLine($"x = {x}");
        x++;
        run25(ref x, ref y);
    }
    if (y <= 25)
    {
        Console.WriteLine($" >> y = {y}");
        y++;
        x = 1;
        run25(ref x, ref y);
    }
    else return ;
}
public static void Main (string[] args) 
{
    int _x = 1;
    int _y = 1;
    run25(ref _x, ref _y); // once it's done , _x and _y values are 26 .
}