对重新调整java代码的括号的改进

pu82cl6c  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(209)

这是我的作业代码。任务是以括号序列的形式获取用户输入,后跟一个e来标记其结束,并确定它是否是一个正确的序列[例如(()())e是corect,)(()((e是不正确的),如果不正确,则确定在开始处需要删除多少括号,在结束处添加多少括号才能正确。
代码起作用了。但它很长,我想知道是否有什么改进我可以做的。非常感谢您的帮助。

public class Main {

  public static String enter(){     
    String enter = "";
    char nextValue ='A';
    while (nextValue!='E'){
      nextValue = In.readChar();
      enter += nextValue;
    }
    return enter;
  }
// User Input. I know this could be done with ArrayList or scanner, but we haven't had these yet in classes. 

//So I did it by converting the user inputs into a string, 

//then storing that string into an array in main with "char []sequence = enter.toCharArray();"

  public static char[] removeParenthesisPairs(char[] a){
    for(int i=0; i<a.length; i++){
      if(a[i]=='('){
        a[i]='F';
        outerloop:
        for(int j=i+1; j<a.length;j++){
          if(a[j]==')'){
            a[j]='F';
            break outerloop;
          }
          if(j==a.length-1){
            a[i]='(';
          }
        }
      }
    }
    return a;
  }
// I remove all the correct pairs from the sequence. I look for a ( and turn it into an F [to signal it's removed] 

//and search the rest of the sequence for a ) and turn that into an F too. If there is no ) I turn the F back into (.

//This part I really don't like, it looks super clunky. Also I have read that the "break outerloop"

//thing I'm using here is bad practice, but I havent figured out any other way to accomplish the same result.

  public static boolean checkIfCorrect(char[] a){
    for(int i=0;i<a.length-1;i++){
      if(a[i]!='F'){
        return false;
      }
    }
    return true;
  }
// I check if the sequence is correct. If there are only F left in the array, 

//that means all entries were able to form pairs of () and the original sequence was correctly parenthesised.

   public static int subtract(char[] a){
    int count = 0;
    for(int i=0; i<a.length;i++){
      if(a[i]==')'){
        count += 1;
      }
    }
    return  count;
  }
// Check how many ) have to be subtracted at the beginning of the sequence.

  public static int add(char[] a){
    int count = 0;
    for(int i=0; i<a.length;i++){
      if(a[i]=='('){
        count += 1;
      }
    }
    return count;
  }
// Check how many ( are still left in the sequence and need a ) added to form a pair.

  public static void main(String[] args) {
    String enter= enter();
    char []sequence = enter.toCharArray();
    removeParenthesisPairs(sequence);
    boolean correct = checkIfCorrect(sequence);
    int additional = add(sequence);
    int removed = subtract(sequence);
    Out.println(correct);
    Out.println("removed = " + removed + " und additional = " + additional);
  }
}
wlzqhblo

wlzqhblo1#

如果给定字符串的任何部分(从索引0到任何索引)中的“(”小于“)”数,则序列不正确。所以。。。只需要一个“数”,见面时加一个“(“见面时减一”)”:

public static boolean works(String str) {//The string I meant here is like "()()()(())" or ")()()()(()))((())". Not "(())(((((E"
        int cnt = 0;//count the number of "(" appear and ")" appear

        for(int i = 0;i<str.length();i++) {
            if(str.charAt(i) =='(') {
                cnt++;//add 1
            }else {
                cnt--;//subtract 1
            }

            if(cnt < 0) {//There is more ")"s than "("s
                return false;//Doesn't work;
            }
        }
        return cnt == 0;//In case the numbers of "(" appear and the numbers ")" appear are different
    }

相关问题