2013-08-27 23 views
-2

我是Java新手,試圖完成一個程序,該程序將讀取用戶的語句,並掃描以查看左括號的數量是否與RIGHT匹配。開始該程序的人創建了一個堆棧,但從未使用過它,所以我把它放在一邊,因爲我對堆棧不太熟悉。但是,我能夠創建一個循環來遍歷字符串中的每個字符以找到括號,比較它們,然後打印出它們是否平均。但是,我遇到了通過字符串查找所有括號的while循環的問題。由於某種原因,它不工作,我不明白爲什麼。任何解釋如何使這項工作將不勝感激。從Java中的字符串中挑選括號

import java.util.* 
public class ParenMatch 
     { 
public static void main (String[] args) 
    { 
Stack s = new Stack(); 
String line; // the string of characters to be checked 
Scanner scan = new Scanner(System.in); 
System.out.println ("\nParenthesis Matching"); 
System.out.print ("Enter a parenthesized expression: "); 
line = scan.nextLine(); 

char parenline[] = new char[line.length()]; 

int x; 
while(x < parenline.length) { 
parenline[x] = line.charAt(x); 
     x++; 
    } 
int l,r,i,morel,morer = 0; 
while (i > parenline.length) { 
     if (parenline[i] == "(") 
      l++; 
     if (line.charAt(i) == ")") 
      r++; 
     i++; 
    } 

    if (l > r) { 
     morel = l-r; 
     System.out.println("There are " +morel+ " more left parentheses than right"); 
    } 

    if (r > l) { 
     morer = r-l; 
     System.out.println("There are " +morer+ " more right parentheses then left"); 
    } 
    if (r == l) { 
     System.out.println("The amount of left and right parentheses are even."); 
    } 
} 

}

+0

定義「不工作」。在我看來,這個代碼將不能編譯,對於那些完全清楚的錯誤消息,我想你會得到解釋的原因,但它是誰也說不準 – EJP

+0

可能的重複[如何在Java中比較字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Kon

+0

@Kon我不知道我不這麼認爲。 –

回答

0

需要初始化X這樣的例子。

int x = 0; 

您不能增加未初始化的變量。 ,來定義parenline,而不是循環,並在只使用的串本地方法一個字符串中的位置添加字符:

char parenline[] = line.toCharArray(); 

很抱歉,如果我解釋說這不好。

0

你有以下錯誤:

  1. 使用,而不是單引號
  2. 檢查雙引號我是否比parenline.length greather未初始化

這是正確的代碼區塊:

int x=0; 
... 

int l,r,i,morel,morer; 
l=r=i=morel=morer= 0; 
while (i < parenline.length) { 
    if (parenline[i] == '(') 
     l++; 
    if (line.charAt(i) == ')') 
     r++; 
    i++; 
} 
+0

kaisernahid,你說得對,我覺得自己像一個白癡r缺少初始化,我很確定我知道在循環的條件下,需要單引號。這是我堅定不移的舉動。謝謝你清理那個。它有所幫助,而且我的代碼現在可以運行 –

+0

錯誤消息中有足夠的信息告訴你所有這些。 – EJP

+0

除了這個之外,所有編譯錯誤都是:i> parenline.length。順便說一句,這隻會匹配括號的總數,而不是像@Vasilina所說的那樣。 – kaisernahid

0

我對代碼做了一些更改,它工作正常。 但是,使用Stack的方法更好,因爲它允許您不僅可以查看括號的大小是否相等,而且可以查看錶達式是否正確。例如,如果你有類似的東西:(x + y))+(x-(y + x),那麼你的程序不能說這是一個不正確的表達式,因爲開括號和閉括號的數量是相等的。

import java.util.*; 
public class Stackpr { 
    public static void main (String[] args) 
    { 
    String line; // the string of characters to be checked 
    Scanner scan = new Scanner(System.in); 
    System.out.println ("\nParenthesis Matching"); 
    System.out.print ("Enter a parenthesized expression: "); 
    line = scan.nextLine(); 
    int l = 0; 
    int r = 0; 
    for (int i = 0; i < line.length(); i++){ 
    if (line.charAt(i) == '(') 
     l++; 
    else if (line.charAt(i) == ')') 
     r++; 
     } 
    if (l > r) 
    System.out.println("There are " + (l-r) + " more left parentheses than right"); 
    else if (l < r) 
    System.out.println("There are " + (r - l)+ " more right parentheses then left"); 
    else 
    System.out.println("The amount of left and right parentheses are even."); 

} 

}