我是java的新手,无法弄清楚如何解决这个简单的问题:
“使一个给出两个正整数的模块返回相同位置的公共位数。”
我需要在不使用数组的情况下制作这个Java方法!我该怎么做呢?提前谢谢!
答案 0 :(得分:3)
您可以使用模数(%
)和除法(/
)运算符来迭代数字:
public static int countCommonDigits(int a, int b) {
int count = 0;
while (a > 0 && b >0) {
// Get the right most digits of each number
int digitA = a % 10;
int digitB = b % 10;
// compare them
if (digitA == digitB) {
++count;
}
// move on to the next digit
a /= 10;
b /= 10;
}
return count;
}