recursion - how convert recursive method to nonrecursive in java -


I am trying to get a combination of five digits, which is equal to 20 and whose average value is certain The value is equal to.

My code is

  package other; Import java.util.ArrayList; Import java.util.list; Import java.util.Random; Public Category Random Numbers {/ ** * @ Ultimate Arguments * / // Maximum Avg = 3.6 /// Minimum Average = 2.2 Public Fixed Zero Main (String [] Args) {// Tudo Auto-Generated Method Stub List & lt ; Integer & gt; I get = (); } Public static list & lt; Integer & gt; getNumbers () {List & lt; Integer & gt; n = new arrelisted & lt; Integer & gt; (); Random R = New Random (); {Int rr = r.nextInt (10); for int (i = 0; i <5; i ++); n.add (rr); } Int sum = 0; Double average = 0; For (int j = 0; j & lt; n.size (); j ++) {sum + = n.get (j); Average + = ((j + 1) * n.get (j)); Println (n.get (j)); } System.out.println ("average:" + average / 20); If (zodiac == 20 & (average / 20) == 2.1) {getCombination (n); } And {getNumbers (); } Return n; } Public static zero getCombination (list & lt; integer & gt; n) {int total = 0; For (int i = 0; i & lt; n.size (); i ++) {total + = n.get (i); If (n.get (i) == 0) {getNumbers (); } Else {System.out.println ("Items:" + i + ":" + n.get (i)); }} System.out.println ("Total:" + total); }}   

It works fine from 2.2 to 3.6 for average values. When we give the second number which is not between 2.2 and 3.6, then this error is giving

  the thread in the exception "main" java.lang.StackOverflowError at sun.nio.cs. SingleByte.withResult (unknown source) sun.nio.cs.SingleByte.access $ 000 (unknown source) sun.nio.cs.SingleByte $ encoder.IncodeArere Loop (unknown source) sun.nio.cs.SingleByte $ encoder.In Code loop (unknown source) `package other;   

I searched in some sites and some of them suggest that avoid recursive and use iterator. But I am in the illusion that how to use this program Iterator.

Any help my project requires will be appreciated. Please help me.

This is not a general answer to change the recurrence in repetition. It's just solving bugs in your code.

  1. Get rid of getCombination () , it does not do anything useful if it is important that there is no zero, at least the method is called "re-inventory Call like "Irenizoros" or just improve the generation logic (see point 2).
  2. Make a method generListOfNumbers () which generates a list whose sum is 20. There is no need to use five completely random numbers and hope; Make the argument better so that (for example)
    1. the first number can be mostly random (0 to 16, may be);
    2. Add something above the second, third and fourth numbers, when added to the current total, (20 - current total);
    3. The fifth number, which is necessary to get up to 20.
    4. Method Boolean isvarigarange (double flooring, double roof) which provides interesting arguments that you have implemented ( average + = ((J + 1) * n.get (j));
    5. Convert the main method to the loop, generListOfNumbers () to call isAverageInRange ( ) Returns is correct.

      As an additional exercise, you can see the weak () and elongation () Instead of creating a new one every time you can create the current list. It can be a significant number of credits from your teacher, I can bet.

      For future reference, there is another stack exchange site that can be useful, if you work your code But want to improve it. / P>

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -