好的,我们假设我们有一个带有这样的数组的变量
int[] digit;
int x = 5;
for(int i=0;i < = 5; i++)
{ digit[i] = 0;}
var数字上的所有值数组都有0.所以我想要做的是我想使用增加增量的按钮控件来增加该数字的值(这意味着数字[4]到数字[3]等等,当数值在数字[4]中达到某个数字示例5时,它将返回到值0并且下一个var数字递增(数字[3])。并且再次开始增加等等。
我已经尝试使用if并切换到这样发生
private btnClick_Click(Object Sender, Event Args)
{
digit[4] +=1;
if(digit[4] > 5) { digit[3] += 1;}
if(digit[3] > 5) { digit[2] += 1;}
//and so on
switch(digit[4])
{
case 5: digit[4]=0;
}
//and so on
}
但它仅用于逻辑如果我们知道数组编号位置。如果我在15位数之类的地方检索该号码。如果我们在命令按钮上设置的数组很少,它就无法正确填充数组?
Imma已经混淆了这个想法,任何建议,帮助,讨论都不理解它。感谢。
答案 0 :(得分:1)
如果您只想增加一个,而不是减去任何减法或增量,我想使用这样一个简单的解决方案:
private void btnClick_Click(Object Sender, Event Args) {
int maxValue = 5;
int carry = 1; // This is our increment
// iterate through your digits back to front
for(int i = digit.Length - 1; i >= 0; i--) {
digit[i] += carry; // increase the value by the carry. This will at least increment the last digit by one
carry = 0;
// if we reach our max value, we set the carry to one to add it to the next digit, and reset our current digit to 0.
// If you wanted to increase by more than 1 a time, we would have to add some more calculations here as it would
// be incorrect to just reset digit[i] to 0.
if(digit[i] >= maxValue) {
carry = 1; // the next digit will now be told to increase by one - and so forth
digit[i] = 0;
} else {
break; // This will break out of the for - loop and stop processing digits as everything just fit nicely and we don't have to update more previous digits
}
}
}
一旦你达到44444
并递增,你就会得到00000
。