forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise.java
More file actions
33 lines (25 loc) · 794 Bytes
/
Copy pathExercise.java
File metadata and controls
33 lines (25 loc) · 794 Bytes
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
/**
* Exercise on encapsulation and generalization.
*/
import java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string of text, such as a simple math calculation with parentheses: ");
String s = input.nextLine();
openCloseParentheses(s);
}
public static void openCloseParentheses(String s) {
System.out.println(s);
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
count++;
} else if (c == ')') {
count--;
}
}
System.out.println("Number of open parentheses: " + count);
}
}