英文:
Sum of first and last digit
问题
我已经编写了一段代码,在这段代码中,我想要添加第一个和最后一个数字。我的代码涵盖了1位数、2位数和3位数的情况,但不包括4位数。此外,我认为我的代码不够简单和高效,我想要使用一个while循环来处理所有情况。有人可以帮忙吗?
public static void main(String[] args) {
System.out.println(sumFirstAndLastDigit(10));
}
public static int sumFirstAndLastDigit(int number) {
int mod = 0;
int firstdivide = 0;
int seconddivide = 0;
int sum = 0;
if (number < 0) {
return -1;
} else if (number > 0 && number < 10) {
return number + number;
} else if (number >= 10 && number < 100) {
mod = number % 10;
firstdivide = number / 10;
sum = mod + firstdivide;
return sum;
}
while (number > 0) {
mod = number % 10;
firstdivide = number / 10;
seconddivide = firstdivide / 10;
sum = mod + seconddivide;
break;
}
return sum;
}
英文:
I have written a code where I want to add the first and last digit. My code covers scenarios of 1 digit and 2 digits and 3 digits, but not 4 digits. Also, I don't think my code is very simple and efficient, I want to use a while loop to cover all scenarios. Can anyone help, please?
public static void main(String[] args) {
System.out.println(sumFirstAndLastDigit(10));
}
public static int sumFirstAndLastDigit(int number) {
int mod = 0;
int firstdivide = 0;
int seconddivide = 0;
int sum = 0;
if (number < 0) {
return -1;
} else if (number > 0 && number < 10) {
return number + number;
} else if (number >= 10 && number < 100) {
mod = number % 10;
firstdivide = number / 10;
sum = mod + firstdivide;
return sum;
}
while (number > 0) {
mod = number % 10;
firstdivide = number / 10;
seconddivide = firstdivide / 10;
sum = mod + seconddivide;
break;
}
return sum;
}
</details>
# 答案1
**得分**: 3
用一点数学,你可以轻松地提取一个数字的第一个和最后一个数字。最后(最右边)的数字可以通过取模来得到,而第一个(最左边)的数字可以通过计算以10为底的对数来获得。
```java
public static int sumFirstAndLastDigit(int number) {
if (number < 0) {
return -1;
}
int lastDigit = number % 10;
int digits = (int) Math.log10(number); // 数字的位数减1
int firstDigit = (int) (number / Math.pow(10, digits));
return lastDigit + firstDigit;
}
英文:
With a little bit of math, you can easily extract the first and last digit of a number. The last (rightmost) digit is simple with modulo, the first (leftmost) digit can be obtained by calculating the base 10 logarithm of the number.
public static int sumFirstAndLastDigit (int number) {
if (number < 0) {
return -1;
}
int lastDigit = number % 10;
int digits = (int)Math.log10(number); // number of digits - 1
int firstDigit = (int)(number / Math.pow(10, digits));
return lastDigit + firstDigit;
}
答案2
得分: 2
你的 while
循环不正确。它将在执行 1 次迭代后总是中断。
if (number < 0) {
return -1;
}
// 获取个位数
int mod = number % 10;
// 不断除以 10,直到只剩下 1 位数字
while (number > 9) {
number /= 10;
}
return mod + number;
英文:
Your while
loop is not correct. It will always break
after 1 iteration.
if (number < 0) {
return -1;
}
// Get ones digit
int mod = number % 10;
// Keep dividing until only 1 digit left
while (number > 9) {
number /= 10;
}
return mod + number;
答案3
得分: 1
以下是翻译好的部分:
private static int sumOfFirstAndLastDigits(final int input) {
int number = Math.abs(input);
final int lastDigit = number % 10;
while (number > 9)
number = number / 10;
return number + lastDigit;
}
英文:
How about this simple one:
private static int sumOfFirstAndLastDigits(final int input) {
int number = Math.abs(input);
final int lastDigit = number % 10;
while (number > 9)
number = number / 10;
return number + lastDigit;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论