For example, if the value was 4, the pattern would look like this
*
* *
* * *
* * * *
* * *
* *
*
The values are stored in a file entitled prog3.dat which looks like this
4
3
15
26
0
I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem.
Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.
import java.util.Scanner;
import java.io.*;
public class Program3 {
public static void main(String[] args) throws Exception {
int num = 0;
java.io.File file = new java.io.File("../instr/prog3.dat");
Scanner fin = new Scanner(file);
while(fin.hasNext()) {
System.out.println("Please enter an integer");
num = fin.nextInt();
//System.out.println(num);
if(num >=0 && num <= 25)
makePattern(num);
}
}
public static void makePattern(int num) {
if(num >= 0 && num <= 25) {
makePattern(num-1);
for(int i = 0; i < num; i++) {
System.out.print("*" + " ");
}
System.out.println("");
}
}
}
I know I"m still not doing things correctly or even if this is actually recursion? Here is the output I get when I run this code:
Please enter an integer
*
* *
* * *
* * * *
Please enter an integer
*
* *
* * *
Please enter an integer
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
Please enter an integer
Please enter an integer
It appears to be reading the file correctly, but is only printing the top half of the pattern. Also, like I said, I'm not very familiar with recursion, so am not sure if this is actually recursion? Any help would be greatly appreciated.

New Topic/Question
Reply


MultiQuote


|