2017-05-12 3 views
-4
public static void count(ArrayList<Double> list) { 

      if(list.empty()){  
       //how to do this **if list is null, return without doing anything ** 
      } 

    //recursion function 

    if(total(list)>100){ 

    /* 
    if total of list is more than 100, remove the first item and make a recursive call to count. 

     For example, if list = [40, 30, 80]=150, list should become [20, 80]=100 

In the recursive function ,if the total of the array is greater than 100 ,using the function we have to remove one of any item and give a value that makes 100 with the rest of the numbers in the array. 
if list = [40, 30, 80]=total is 150 ,so it is greater than 100 
then list should become [20, 80]=100 (removed first two item and it replace by 20) 


    */ 

    } 

    } 

注空の戻りです:合計のためのいずれかの方法を作るために 必要がありません私はまだ(ダブルトータルにこれを使用するものを作成しましたArrayListリスト)は、再帰関数の合計を呼び出します。アルゴリズムArrayListのための再帰関数のとArrayListのは、正確な配列

+2

してください具体的な問題を明確にしたり、詳細を追加して必要なものを正確に強調したりできます。現在書かれているとおり、あなたが求めていることを正確に伝えるのは難しいです。この質問を明確にするために[** How to Ask page **](http://stackoverflow.com/help/how-to-ask)を参照してください。 –

+0

まず、 'List 'の合計をとるメソッドが必要です。 – Rogue

+0

あなたの質問は不明です。 [40、30、80] - > [20、80] –

答えて

0

私はあなたが求めているものを正確に理解していれば、このようなものは、それを満たすが、再帰は本当にあなたが明らかに何をしようとして達成するための最良の方法ではないことに注意してくださいする必要があります

public static void count(ArrayList<Double> list) { 
    if (list.empty()) {  
    return; // or consider using if/else 
    } 

    double total = total(list); 

    if (total>100) { 
    list.remove(0); 
    count(list); // recursive call 
    } else if (total<100) { 
    list.add(0, 100-total); 
    } 
} 
+0

まさにあなたのものを使って私の要求に応えてくれました。 –

関連する問題