| | |
Java and recursion
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2006
Posts: 2
Reputation:
Solved Threads: 0
i am working on a program where the user enters a number and the computer will generate N number of characters and print them in all combonations. For example:
enter number: 3
aaa aab aac aba abb abc aca acb acc
baa bab bac bba bbb bbc bca bcb bcc
caa cab cac cba cbb cbc cca ccb ccc
I have made the program build the array and randomize the characters however i dont know how to use recursion to print all the combos. Here is what i have so far:
import cs1.Keyboard;
import java.util.Random;
public class Letter
{
public static void main(String[] args)
{
System.out.println("How many characters do you want");
int number = Keyboard.readInt();
char[] list = new char[number];
Random generator = new Random();
for(int index = 0; index < list.length; index++)
{
int temp = generator.nextInt(26) + 65;
list[index] = (char) temp;
}
//check if all characters are different
for(int index = 0; index < list.length-1; index++)
{
if(list[index] == (list[index+1]))
{
int temp = generator.nextInt(26) + 65;
list[index+1] = (char) temp;
index=0;
}
}
I believe recursion would be the best solution but i dont know how to use it to print the answer. Any help would be great. thanx
enter number: 3
aaa aab aac aba abb abc aca acb acc
baa bab bac bba bbb bbc bca bcb bcc
caa cab cac cba cbb cbc cca ccb ccc
I have made the program build the array and randomize the characters however i dont know how to use recursion to print all the combos. Here is what i have so far:
import cs1.Keyboard;
import java.util.Random;
public class Letter
{
public static void main(String[] args)
{
System.out.println("How many characters do you want");
int number = Keyboard.readInt();
char[] list = new char[number];
Random generator = new Random();
for(int index = 0; index < list.length; index++)
{
int temp = generator.nextInt(26) + 65;
list[index] = (char) temp;
}
//check if all characters are different
for(int index = 0; index < list.length-1; index++)
{
if(list[index] == (list[index+1]))
{
int temp = generator.nextInt(26) + 65;
list[index+1] = (char) temp;
index=0;
}
}
I believe recursion would be the best solution but i dont know how to use it to print the answer. Any help would be great. thanx
This is the basic principle...
From there you should be able to devise a general model and see where the recursion comes into play.
Java Syntax (Toggle Plain Text)
char stuff[]="abc"; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { for(int k=0; k<3; k++) { print stuff[i]; print stuff[j]; print stuff[k]; print newline } } }
From there you should be able to devise a general model and see where the recursion comes into play.
*Voted best profile in the world*
try the following:
Java Syntax (Toggle Plain Text)
public void printLoop(int index; char[] list; char[] string) { if (index == list.length) { System.out.print(new String(string) + " "); return; } for (int i = 0; i < list.length; i++) { string[index] = list[i]; printLoop(index + 1, list, string); } } char[] string = new char[list.length]; for (int i = 0; i < list.length; i++) { string[0] = list[i]; printLoop(1, list, string); System.out.println(""); }
![]() |
Similar Threads
- Recursion (Java)
- number formatting using recursion (Java)
- Syntax Analyser and General Java Help (Java)
- Java Expert (Needed) (Java)
- Java Chat Reboots (Java)
Other Threads in the Java Forum
- Previous Thread: Simple question about using JDBC to access DB2
- Next Thread: bit of a search problem
Views: 3685 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation balls binary bluetooth business chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging draw ebook eclipse educational error event exception file fractal game givemetehcodez graphics gui helpwithhomework hql html ide image ingres input integer invokingapacheantprogrammatically j2me java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number numbers object oracle parameter php print problem program programming project recursion scanner screen server set size sms socket sort sql string sun swing swt tcp test threads time transfer tree udp windows






