-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayProcessing.java
More file actions
84 lines (64 loc) · 2.56 KB
/
ArrayProcessing.java
File metadata and controls
84 lines (64 loc) · 2.56 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
82
83
84
/************************************************************/
/*Program: Array Processing */
/*CIS163AA 31892 */
/*Marc Holley */
/*4/10/2016 */
/*this program demonstrates array processing */
/************************************************************/
import java.util.Scanner;
import java.util.InputMismatchException;
// Class
public class ArrayProcessing {
// method call with passby reference
public static void positiveOrNegative (int numInput) {
// declaring and intializing string array
String[] types = {"positive", "negative"};
// conditional statements with logical operands
if (numInput > 0) {
System.out.println("Your number is " + types[0]);
}
else {
System.out.println("Your number is " + types[1]);
}
}
public static void getNumber() {
// declaring and intializing string array
String[] types = {"even", "odd", "Invalid Number"};
// declaring and initalizing variable(s)
int numInput;
// constructing new scanner class
Scanner scnr = new Scanner(System.in);
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 new InputMismatchException("Invalid number.");
}
// conditional statement with logical operands
if (numInput % 2 == 0) {
System.out.println("Your number is " + types[0]);
}
// conditional statement with logical operands
else {
System.out.println("Your number is " + types[1]);
}
// 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();
}
}