如何在Java中计算整数的两倍数字?

huangapple go评论78阅读模式
英文:

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 &lt; 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 &gt; 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.

huangapple
  • 本文由 发表于 2020年10月10日 04:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64286975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定