英文:
How to calculate the double of digits from an integer in Java?
问题
我是Java的初学者,正在努力学习。我有一个整数,我想计算每个数字的两倍,然后将答案恢复到整数中。我认为我必须使用for()循环和tempvalue。我有数字1234,我需要得到2468。
我这样做了,但我得到了1234。有人能找到问题吗?我对索引的概念不是很了解。
public class Doublevalue {
public static void main(String[] args) {
int num = 1234;
int newNum = 0;
int multiplier = 1;
while (num > 0) {
int digit = num % 10;
int doubledDigit = digit * 2;
newNum += doubledDigit * multiplier;
multiplier *= 10;
num /= 10;
}
System.out.println(newNum);
}
}
英文:
I am a beginner in Java and trying to learn. I have an integer from whom I want to calculate the double of each digit and then restore the answer in the integer. I think that I have to use the for() loop and tempvalue. I have the number 1234 and I need to get 2468.
I did this and I got 1234. Can someone find the issue, I am not very good with the index concept.
public class Doublevalue {
public static void main(String[] args) {
num=1234;
for(int i=0;num<0;i++) {
int tempvalue=(num%10*2)/10;
num=tempvalue;
System.out.print(num);
}}}
答案1
得分: 0
你目前提供的代码除了循环部分以外是正确的。
在你的示例中,你执行了int tempvalue=(num%10*2)/10;
。我相当确定你不确定自己在做什么。这一行的作用是在将数字除以10得到余数后,将其乘以2,然后再除以10。我似乎无法理解你为什么这样做,所以我将提供我自己的解决方案。
public class DoubleDigits {
public static void main(String[] args) {
DoubleDigits dd = new DoubleDigits();
System.out.println(dd.doubleDigits(1234));
}
public int doubleDigits(int number) {
StringBuilder str = new StringBuilder();
String testCase = String.valueOf(number);
for(int i = 0; i < testCase.length(); i++) {
int digit = Integer.parseInt(String.valueOf(testCase.charAt(i))) * 2;
str.append(digit);
}
return Integer.parseInt(str.toString());
}
}
那么发生了什么?
首先,我们将数字转换为字符串,以便我们可以获得每个单独的字符(作为一个数字)。for 循环将遍历每个单独的字符,我们可以使用 StringBuilder,在将字符解析为 int 并乘以两倍后将其附加上去。
在上面的示例中,该程序生成:
2468
当测试案例为:
1234
以及测试案例为:
9999
结果为:
18181818
英文:
The code you currently provided has everything right - except for what you are doing in the loop.
In your example, you are executing int tempvalue=(num%10*2)/10;
. I'm pretty certain that you are not sure what you are doing. What the line is doing is getting the remainder of the number when it is divided by 10, multiplying it by 2, then dividing by then. I can't seem to understand why you are doing this, so I will provide my own solution.
public class DoubleDigits {
public static void main(String[] args) {
DoubleDigits dd = new DoubleDigits();
System.out.println(dd.doubleDigits(1234));
}
public int doubleDigits(int number) {
StringBuilder str = new StringBuilder();
String testCase = String.valueOf(number);
for(int i = 0; i < testCase.length(); i++) {
int digit = Integer.parseInt(String.valueOf(testCase.charAt(i)))*2;
str.append(digit);
}
return Integer.parseInt(str.toString());
}
}
So what's happening?
First, we convert the number to a String, so we can get each single character (as a number). The for loop will loop through every single character, and we can use a StringBuilder to append the character after it has been parsed to an int and multiplied by two.
In the above example, the program produces:
2468
When the test case is:
1234
And when the test case is:
9999
The result is:
18181818
答案2
得分: 0
因为只有在每个数字都小于5的情况下,翻倍数字才有效,解决方案可以简单地是:
num *= 2;
但是,如果你想要分别处理每个数字,你需要像这样做:
int tmp = 0;
for (int column = 1; num > 0; column *= 10) {
tmp += (num % 10) * column * 2;
num /= 10;
}
num = tmp;
详见演示链接。
你也可以就地替换每个数字,但逻辑会稍微复杂一些。
英文:
Because doubling digits is only valid when every digit is less than 5, the solution can be just
num *= 2;
But if you want the treat each digit separately, you need to do something like this:
int tmp = 0;
for (int column = 1; num > 0; column *= 10) {
tmp += (num % 10) * column * 2;
num /= 10;
}
num = tmp;
See live demo.
You can do an in-place replacement of each digit, but the logic is a little more complicated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论