-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.java
More file actions
68 lines (56 loc) · 2.02 KB
/
Recursion.java
File metadata and controls
68 lines (56 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Import modules.
import java.lang.NumberFormatException;
import java.util.Scanner;
/** Copyright (c) 2022 Mel Aguoth All rights reserved.
* This produces a numeric pattern symmetric about n (a user-inputted integer) where each half
* before and after n is also symmetric around n-1.
*
* @author Mel Aguoth
* @version 11.0.13
* @since 2022-02-01
*/
final class Recursion {
private static void numberPattern(final int funcInt) {
// If the user's integer is 1, display 1.
if (funcInt == 1) {
System.out.print(" " + funcInt + " ");
// If the user's integer is above 1, display the pattern for their integer.
} else {
numberPattern(funcInt - 1);
if (funcInt > 5) {
System.out.println(funcInt);
} else {
System.out.print(funcInt);
}
numberPattern(funcInt - 1);
}
}
public static void main(final String[] args) {
// Declare an empty string for a NumberFormatException.
String userString = "";
try {
// Introduce the program.
System.out.println("This program displays a pattern using a user-inputted integer."
+ " The integer has to be between 1 and 15.");
// Get the user's integer.
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
userString = input.next();
final int userInt = Integer.parseInt(userString);
input.close();
// If the user's integer isn't between 1 and 15, display an error to the user.
if (userInt < 1 || userInt > 15) {
System.out.println("\n" + userInt + " isn't between 1 and 15. Please try again.");
} else {
// Call numberPattern.
System.out.println();
numberPattern(userInt);
System.out.println();
}
// If the user's input isn't an integer, display an error to the user.
} catch (NumberFormatException e2) {
System.out.println("\n" + "'" + userString + "' isn't an integer."
+ " Please enter a proper integer.");
}
}
}