2014-12-07 83 views
-3

如果輸入x = y + 5檢查我的邏輯錯誤,請

,輸出必須是:

Token 1 is x is identifier 
Token 2 is = 
Token 3 is y is identifier 
Token 4 is + 
Token 5 is 5 

,這是我沒有我不知道什麼是錯在我,如果條件代碼!

char[] ident = {'x','y','z','a','b','c','k'}; 

Scanner sc = new Scanner(System.in); 
System.out.println("Please Enter Your String"); 
String x = sc.nextLine(); 


StringTokenizer t = new StringTokenizer(x); 

for (int k = 0; k < ident.length; k++) { 
    for (int j = 1; j < x.length()+1; j++) { 
     char m = x.charAt(j); 


     if (m==ident[k]) { 

      System.out.println("Token " + j + " is " + t.nextToken()+" is identifier"); 

     } else { 
      System.out.println("Token " + j + " is " + t.nextToken()); 
     } 

    } 

} 
+0

有什麼問題? – SMA 2014-12-07 17:04:47

+0

運行代碼知道問題:D – 2014-12-07 17:13:31

回答

0

你不需要兩個循環nether StringTokenizer。你可以這樣做:

final String str = new String(ident); 
char m; 
for (int j = 0; j < x.length(); j++) { 
    m = x.charAt(j); 
    if (str.indexOf(m) >= 0) { 
     System.out.println("Token " + j + " is " + m+" is identifier"); 
    } else { 
     System.out.println("Token " + j + " is " + m); 
    } 
} 
+0

omg ty! :D和好主意不要使用字符串標記符^ _^ – 2014-12-07 17:22:19

+0

不客氣。如果您認爲它有幫助,請接受答案來關閉此問題,以便其他人可以在不久的將來將其用作參考。 – SMA 2014-12-07 17:23:25

+0

完成mate thx再次:) :) – 2014-12-07 17:27:09