-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodsAndRecursion.java
More file actions
81 lines (63 loc) · 2.39 KB
/
MethodsAndRecursion.java
File metadata and controls
81 lines (63 loc) · 2.39 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
69
70
71
72
73
74
75
76
77
78
79
80
81
/************************************************************/
/*Program: Methods and Recursion */
/*CIS163AA 31892 */
/*Marc Holley */
/*4/10/2016 */
/*this program demonstrates methods and recursion */
/************************************************************/
import java.util.Scanner;
import java.util.InputMismatchException;
// Class
public class MethodsAndRecursion {
// method with a passby reference
public static void positiveOrNegative (int numInput) {
// conditional statement with logical operands
if (numInput > 0) {
System.out.println("Your number is positive.");
}
else {
System.out.println("Your number is negative.");
}
}
// method
public static void getNumber() {
// declaring and initalizing variable(s)
int numInput;
// constructing new scanner class
Scanner scnr = new Scanner(System.in);
// try statement
try {
// prompting user for input
System.out.println("Enter a even or odd number: ");
numInput = scnr.nextInt();
// conditional statement with logical operands
if (numInput % 2 != 0 && numInput % 2 != 1) {
// throw statement
throw new InputMismatchException();
}
// conditional statement with logical operands
if (numInput % 2 == 0) {
System.out.println("Your number is even.");
}
// conditional statement with logical operands
else {
System.out.println("Your number is odd.");
}
// method call with passby reference
positiveOrNegative(numInput);
}
// catch statement recieves throw
catch (InputMismatchException exception) {
// outputs error messages
System.out.println("Invalid input!");
System.out.println("Must be an integer.");
// recursive method call
getNumber();
}
}
// main method
public static void main(String[] args) {
// method call
getNumber();
}
}