java 尝试将单个 int 数字拆分为数组的单独数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15421896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:31:48  来源:igfitidea点击:

Trying to split a single int number into separate numbers for an array

javaarraysstringinteger

提问by Joseph Mindrup

I know you can do this using characters with the .charAt()line but I'm wondering if there is anything like that for an Integer? This is in java.

我知道你可以使用带有.charAt()行的字符来做到这一点,但我想知道整数是否有类似的东西?这是在java中。

EDIT:

编辑:

This is the code im tring to do this to:

这是我要执行此操作的代码:

for(int j=0; j<lines; j++){ 
        for (int k=0; k<quizKey.length; k++){
        String lane = userAnswers   
            userIndividualAnswers[k][0] = userAnswers[0].IntAt(k);

        }//end for loop
    }//end for loop

obviously this is incorrect but using the math method how would i be able to convert the userAnswers ints separately? the userAnswers aray has like 5 ints in it.

显然这是不正确的,但使用数学方法我如何能够分别转换 userAnswers 整数?userAnswers 数组中有 5 个整数。

回答by Ron Dahlgren

A multi-digit number is only multi-digit in the sense that the String representation requires multiple digits. With that in mind, your best option will be to use toString and pull out the individual components as Integers.

在字符串表示需要多位数字的意义上,多位数字只是多位数字。考虑到这一点,您最好的选择是使用 toString 并将单个组件作为整数拉出。

Or, as a commenter above mentioned, use integer division and % 10 to do it with math.

或者,作为上面提到的评论者,使用整数除法和 % 10 来进行数学运算。

回答by robbert229

If you have an integer I and let's say I = 19478. if we use I%10, we get 8. % is used as modulos. It returns the remainder of the division. A good reference to modulos is -> Wikipedia. An implementation of mod in your situation is the following.

如果你有一个整数 I,假设 I = 19478。如果我们使用 I%10,我们得到 8。% 用作模数。它返回除法的余数。模数的一个很好的参考是 -> Wikipedia。您的情况下的 mod 实现如下。

  int target = 104978;
  int[] ara = new int[6];         

  for(int i=0;i<ara.length;i++)
  {
         ara[i]=target%10; 
         target=target/10;
  }

First time through it will mod 104978 % 10 and return 8. It will then do integer division on 104978/10 and set the target to 10497. Second round through it mods 10497 % 10 = 7, etc. I hope this was helpful.

第一次通过它将 mod 104978 % 10 并返回 8。然后它将对 104978/10 进行整数除法并将目标设置为 10497。第二轮通过它 mods 10497 % 10 = 7,等等。我希望这会有所帮助。