Created
February 10, 2017 16:20
-
-
Save npruehs/a424b6c286f5b5af26975162fae52d02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Aufgabe9 { | |
public static void main(String[] args) { | |
System.out.println(korrektGeklammert("2*(3+(4+6))")); | |
System.out.println(korrektGeklammert("17+50]*6")); | |
System.out.println(korrektGeklammert("(3*[3+17)+2]")); | |
System.out.println(korrektGeklammert("(12/6)[1/3]")); | |
} | |
public static boolean korrektGeklammert(String s) { | |
return korrektGeklammert(s, ' '); | |
} | |
private static boolean korrektGeklammert(String s, char letzteOffeneKlammer) { | |
for (int i = 0; i < s.length(); ++i) | |
{ | |
char zeichen = s.charAt(i); | |
if (zeichen == '(' || zeichen == '[') | |
{ | |
return korrektGeklammert(s.substring(i + 1), zeichen); | |
} | |
else if (zeichen == ')' && letzteOffeneKlammer != '(') | |
{ | |
return false; | |
} | |
else if (zeichen == ']' && letzteOffeneKlammer != '[') | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment