为什么需要重复减1?

这个问题是基于这个线程编程谜语:你怎么可以翻译一个Excel列的名称为一个数字?

这里是从这个问题的代码翻译列号到Excel列名

public String getColName (int colNum) { String res = ""; int quot = colNum; int rem; /*1. Subtract one from number. *2. Save the mod 26 value. *3. Divide the number by 26, save result. *4. Convert the remainder to a letter. *5. Repeat until the number is zero. *6. Return that bitch... */ while(quot > 0) { quot = quot - 1; rem = quot % 26; quot = quot / 26; //cast to a char and add to the beginning of the string //add 97 to convert to the correct ascii number res = (char)(rem+97) + res; } return res; } 

我彻底地testing了这个代码,它的工作原理,但我有一个问题,这条线需要重复这个工作

  quot = quot - 1; 

根据我的理解,需要将“色号”映射到距“a”的距离。 这意味着1应该映射到距“a”0的距离,与“a”2到1的距离等等。 但是,为了解释这个,你不需要减去这个吗? 最终我不是在一个循环中,

  quot = quot / 26; 

将停止循环。

Excel列不是正常的数字系统。 它不仅仅是基地26.第一个两位数列是“AA”。 在任何正常数字系统中,前两位数字由两个不同的数字组成。 基本上,在excel列编号中,没有“零”数字。

为了解决这个差异,每次迭代减1。