我在 Java 中返回類型時遇到問題。該程序是關于將 5 個 chiffres 的整數分解為一個數組,而不使用預定義的方法,只使用經典算法以遞歸方式。我編寫了代碼,它與遞歸配合得很好,但是當我從方法中捕獲新數組時,它在每種情況下都不會返回一個值,而是在一種情況下返回 2 到 5 個 chiffres。我想要實現的是:12345 >> [1][2][3][4][5]但我的算法給了我這個:12345 >> [0][12][123][1234][12345]我想問題出在返回類型或聲明方法中。這是我的代碼:public class recursive { static int[] Decompose (int[] tab , int pos,int num) //pos for position { if (pos == 0) //if it reach the last element of the table it stop { return tab; } else { tab[pos]=num; return Decompose(tab,pos-1,num/10); // if we didn't reach the end continue ... } } public static void main(String[] args) { int []t = new int[5]; int n=12345;//the number that i want to decompose int pos=4;//the indicator of table position t=Decompose(t,pos,n); for (int i = 0; i < t.length; i++) { System.out.println(t[i]); } }}
添加回答
舉報
0/150
提交
取消