-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
35 lines (31 loc) · 976 Bytes
/
Main.java
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
package com.yurii.salimov.lesson08.task02;
/**
* 8.2 Задача
*
* Проверить баланс круглых скобок в выражении.
* Пример: “((x – 8)+a(y – 1))”.
*
* @author Yuriy Salimov ([email protected])
*/
public class Main {
public static void main(String[] args) {
printEnglishAlphabet();
}
private static void printEnglishAlphabet() {
final String line = "((x – 8)+a(y – 1))";
final boolean isCorrectLine = isCorrect(line);
System.out.println(line + " - " + (isCorrectLine ? "correct" : "not correct"));
}
private static boolean isCorrect(final String line) {
int count = 0;
for (int i = 0; i < line.length(); i++) {
final char ch = line.charAt(i);
if (ch == '(') {
count++;
} else if (ch == ')') {
count--;
}
}
return (count == 0);
}
}