C#for循环增量需要帮助 [英] C# for loop increment need help

查看:84
本文介绍了C#for循环增量需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试循环,似乎不能让它正常工作,因为我是C#的新手。



请参阅下文:



Hi
I am trying to loop and cant seem to get it working as I am new to C#.

Please see below:

using System;
					
public class Program
{
	public static void Main()
	{
		for (int j = 0; j < 5; j++)
		{
			int LoopNumber = 0;
			int SquadNumber = 3;
			int DPad_Down;
			LoopNumber++;
			if (LoopNumber % SquadNumber > 0) { DPad_Down = SquadNumber; }
			else { DPad_Down = 1; }

			for (int i = 0; i < SquadNumber; i++)
			{
				Console.WriteLine("DPad_Down", i+1);
			}
			Console.WriteLine("squad {0} selected", SquadNumber);
			SquadNumber++;
		}
	}
}





我得到的结果如下:



DPad_Down

DPad_Down

DPad_Down

小队3选中

DPad_Down

DPad_Down

DPad_Down

小队3选中

DPad_Down

DPad_Down
DPad_Down

小队3选中

DPad_Down

DPad_Down

DPad_Down

小队3选中

DPad_Down

DPad_Down

DPad_Down

小队3选中



我想做的是:

DPad_Down

小队1选中

DPad_Down

DPad_Down

小队2选中

DPad_Down

DPad_Down

DPad_Down

小队3选中

DPad_Down

DPad_Down

DPad_Down

DPad_Down

小队4选择

DPad_Down

DPad_Down

DPad_Down

DPad_Do wn

DPad_Down

小队5选中



我应该修理什么?



请帮忙!



我的尝试:



我试图在互联网上查找。



I get a outcome as below:

DPad_Down
DPad_Down
DPad_Down
squad 3 selected
DPad_Down
DPad_Down
DPad_Down
squad 3 selected
DPad_Down
DPad_Down
DPad_Down
squad 3 selected
DPad_Down
DPad_Down
DPad_Down
squad 3 selected
DPad_Down
DPad_Down
DPad_Down
squad 3 selected

What I am trying to do is:
DPad_Down
squad 1 selected
DPad_Down
DPad_Down
squad 2 selected
DPad_Down
DPad_Down
DPad_Down
squad 3 selected
DPad_Down
DPad_Down
DPad_Down
DPad_Down
squad 4 selected
DPad_Down
DPad_Down
DPad_Down
DPad_Down
DPad_Down
squad 5 selected

What should I fix?

Please help!

What I have tried:

I tried to look up on the internet.

推荐答案

使用调试器快速查看会显示问题所在:

A quick look with the debugger would have shown you what the problem is:
		for (int j = 0; j < 5; j++)
		{
...
			int SquadNumber = 3;
...
			SquadNumber++;
		}

因此每次绕过外循环时,SquadNumber总是相同的 - 三个。

将定义移到循环外部,并将其设置为1相反:

So every time you go round your outer loop the SquadNumber is always the same - three.
Move the definition outside the loop, and set it to one instead:

		int SquadNumber = 1;
		for (int j = 0; j < 5; j++)
		{
...
			SquadNumber++;
		}


您的代码行为不符合预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是帮助你告诉你发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - YouTube [< a href =https://www.youtube.com/watch?v=u-HdLtqEOogtarget =_ blanktitle =New Window> ^ ]

调试器这里只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于C#for循环增量需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆