## 如何在没有Java递归方法的情况下减少两位数的给定数字？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (62)

``````difference between 6 and 9 is 3
difference between 9 and 2 is 7
difference between 2 and 8 is 6
``````

``````difference between 3 and 7 is 4
difference between 7 and 6 is 1
``````

``````public static int twodigit(int value) {
while (value > 99)
value = reduce(value);
return value;
}
private static int reduce(int value) {
return (value<=9? 0:reduce(value/10)*10 + Math.abs(value/10%10-value%10));
}
``````

### 2 个回答

```private static String calculateTwoDigitNumber(String number) {
while (number.length() > 2) {
String tmpNumber = "";
for (int i = 1; i < number.length(); i++) {
tmpNumber += Math
.abs(Integer.parseInt(number.charAt(i - 1) + "") - Integer.parseInt(number.charAt(i) + ""));
}
number = tmpNumber;
}
return number;
}```

```public static void main(String[] args) {
System.out.println(reduce(6928));
}

public static long reduce(long v) {
while(v > 9) {
System.out.println(v);
long y = 0, factor = 1;
// go through each digit from the bottom and calc the diff.
while(v > 9) {
y += factor * Math.abs(v % 10 - v / 10 % 10);
v /= 10;
// each digit is worth 10x the last.
factor *= 10;
}
v = y;
}
return v;
}```

```6928
376
41
3```