Note that there are some explanatory texts on larger screens.

plurals
  1. POCan this be done Recursively?
    text
    copied!<p>So me and my friend tried to code this little game when we were children called LOVERS.. Wherein you write down the name of 2 persons,Whole name without the middle name,and count the number of L's,O's,V's,E's,R's,and S's in the name, add it together and put beside the letters.</p> <p>Sample: <br/> name 1: Hello <br/> name 2: Care <br/> <br/> L: 2 <br/> O: 1 <br/> V: 0 <br/> E: 2 <br/> R: 1 <br/> S: 0 <br/> <br/> afterwards you will add them in pairs. <br/></p> <p>Sample: <br/> L: 2 > 3 > 4 > 7 > 15 > 32 <br/> O: 1 > 1 > 3 > 8 > 17 <br/> V: 0 > 2 > 5 > 9 <br/> E: 2 > 3 > 4 <br/> R: 1 > 1 <br/> S: 0 <br/> <br/> <br/> here's how it goes...first you add the values of the first 2 letters...LO then OV then VE and so on and so forth. until you get one final answer in this case 32....the 32 signifies the percentage in which the 2 people is Compatible with each other. <br/> i know its quite stupid. haha but we just tried to program it for fun. we are 2nd year IT Students here in the philppines. anyway we were wondering if there's a way to do the calculation RECURSIVELY and if there's a way to reduce the number of Arrays used. <br/> Here's our code: <br/> <br/> <br/> <br/></p> <pre><code>import java.util.*; public class LOVERS { static Scanner console = new Scanner(System.in); public static void main(String[] args) { String name1=""; String name2=""; char love[] = {'L','O','V','E','R','S'}; int[] lovers = new int[6]; int[] temp= new int[6]; int[] temp2= new int[6]; boolean done = true; while(done){ name1 = getName(); name2 = getName(); temp = getLetterCount(name1); temp2 = getLetterCount(name2); lovers = sumOfLetters(temp,temp2,love); System.out.println(""); int[] firstLayer = new int[5]; int[] secondLayer = new int[4]; int[] thirdLayer = new int[3]; int[] fourthLayer = new int[2]; firstLayer = sums(lovers); secondLayer = sums(firstLayer); thirdLayer = sums(secondLayer); fourthLayer = sums(thirdLayer); int output = fourthLayer[0]+fourthLayer[1]; if(output&gt;100){ output=100; } System.out.println("Result is : "+ output +"%"); System.out.println("Do you want to try again? Y/N :"); char again = ' '; if(again == 'n') { done = false; } else done = true; } } public static int[] sums (int[] y){ int[] x = new int[y.length-1]; for(int ctr=1;ctr&lt;y.length;ctr++){ x[ctr-1]=y[ctr-1]+y[ctr]; } return x; } public static String getName(){ String n=""; System.out.println("Enter name: "); n = console.nextLine(); n = n.toUpperCase(); return n; } public static int[] sumOfLetters(int[] temp, int[] temp2, char[] love){ int[] lovers = new int[6]; for(int ctr=0;ctr&lt;6;ctr++){ lovers[ctr]=temp[ctr]+temp2[ctr]; System.out.println(love[ctr]+" - "+lovers[ctr]); } return lovers; } public static int[] getLetterCount(String n){ int[] temp = new int[6]; for(int x=0;x&lt;n.length();x++){ if(n.charAt(x)=='L'){ temp[0]++; } else if(n.charAt(x)=='O'){ temp[1]++; } else if(n.charAt(x)=='V'){ temp[2]++; } else if(n.charAt(x)=='E'){ temp[3]++; } else if(n.charAt(x)=='R'){ temp[4]++; } else if(n.charAt(x)=='S'){ temp[5]++; } } return temp; } } </code></pre> <p>as you can see we used 4 arrays for the 4 layers of calculation and we used a looping statement for the calculation. </p> <p>So can this be done RECURSIVELY? and How can we reduce the number of arrays used?</p> <p>this can help us greatly in learning how to do proper Recursive functions since we are currently learning Data Structures. hope you guys can help me. thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload