将数字拆分为单独数字的算法如何工作

how a splitting number to a separate digits algorithm works

我要回到软件开发,我在 java 中研究算法,今天我正在做将一个数字拆分为单独数字的算法,我在这里找到了我在 java 中写了它..它有效但老实说我不知道​​如何?有代码,只是我不明白其中的一部分:

public static void main(String[] args) {
    Integer test = 0, i, N;
    ArrayList array_one= new ArrayList<Integer>();
    Scanner sc = new Scanner(System.in);

    System.out.print("Write An Integer :");
    test = sc.nextInt();
    while (test > 0){
      int mod = test % 10; // <= DON'T UNDERSTAND THE WORK OF THAT PART
                           // (i know it's the modulo of the entered value)
       test = test / 10;
       array_one.add(mod);
    }
    System.out.print(array_one);
}

我知道这是一个新手问题,我只是对软件工程和算法充满热情,只是想知道它到底是如何工作的,提前谢谢。

test % 10; 给出数字的最后一位(最低位),即数字除以 10 的余数。

test = test / 10 将数字减少一位(123456 变为 12345),使前第二个最低有效数字成为新的最低有效数字。因此,在下一次迭代中,test % 10; 将 return 第二个数字。

等等...

通过使用 %10,您只会得到最后一位数字。 /10 将给出最后一位数字之前的内容。

这样你就可以构造你的数组了。

124%10 --> 4
124/10 --> 12   % 10 --> 2
             12 / 10 --> 1
test % 10;  --> Always gives you the last digit.
 test / 10; --> divides the existing number by 10.
 while loop --> executes until test > 0

So, if your number is 234,
234%10 would be 4
234/10 would be 23.4 which will be converted to 23.

Apply 23 % 10 and 23/10 and so on..

举个例子就明白了
你的数字除以它的数字是 345
如果将它除以 10,则剩下的第一个数字是 5

这里使用的逻辑是把个数除以10得到提醒值,先把单位分开。

例如 x=153

“%”是给出除法余数的取模运算符 “/”是只给出商的除法运算符

then 153%10= 3 //这是分隔第一个数字的余数。 然后将数字除以 10 得到商

即 153/10 =15 // 只有商

继续循环,现在 15 作为新的原始数字,再次除以 10 得到余数,从而得到下一个数字。

即 15%10 =5 //下一位 15/10=1;

 1%10=1    //digit
 1/10=0   //The loop ends here