反转while循环



反转while循环,我不知道这是否是正确的提问方式,但是来吧。我有一个代码,可以把任何整数转换成两位或三位小数。如何?将整数值除以2,直到小于10,依次计算在redo变量中除以它的次数,因此我们得到第十次:decimal = (integer, redo):

int InitialInteger = 190;
int Integer = InitialInteger;
int redo = 0;
while (Integer > 10)
{
Integer = Integer / 2;
redo++;
}
//Salida: 5,5

现在我想要返回初始的整数值("反转循环"),也就是说,将整数部分乘以2,乘以小数部分所指示的次数:Integer = (decimal1 * 2)^decimal2

int decimal1 = Int16.Parse(Decimal.Split(',')[0]);
int decimal2 = Int16.Parse(Decimal.Split(',')[1]);
while (decimal2 > 0)
{
redo -= 1;
InitialInteger += decimal2 * 2;
}
//Salida: 60

但是它不起作用,它永远不会返回初始值!任何想法?

请注意,您必须将Integer设置为float而不是int才能正常工作。

用这个代替

int InitialInteger = 190;
float temp = InitialInteger;
int redo = 0;
while (temp > 10)
{
temp= temp / 2;
redo++;
}

,然后反转

while (redo > 0)
{
temp = temp * 2;
redo--;
}

在末尾,temp(或者你称之为Integer !)还是190