我必須刪除文件中的所有註釋。引號內的註釋分隔符應視爲文本,並且必須打印到屏幕上。評論內的評論被視爲其他評論,必須刪除。在switch語句中表示單引號的錯誤
我在遇到下面的switch語句時遇到了單引號問題,它給了我一個錯誤。
我知道錯誤是什麼,但我只是不知道如何解決它。
import java.io.*;
public class Q3{
public static final int STATE_NORMAL = 0;
public static final int STATE_QUOTE_SINGLE = 1; // this:''
public static final int STATE_QUOTE_DOUBLE = 5; // this: ""
public static final int STATE_SLASH = 2; // this:/
public static final int STATE_SLASH_STAR = 3; // this: /* *
public static final int STATE_COMMENT = 4; // this: /* */ || /* only
public static void main(String[] argv) throws IOException{
final int EOF = -1;
InputStreamReader in = new InputStreamReader(System.in);
int c;
char[] buffer = new char[2];
int currState = Q3.STATE_NORMAL;
char outChar;
while((c = in.read()) != EOF){
outChar = (char) c;
switch (outChar){
case '/' :
/* if(currState == Q3.STATE_QUOTES)
currState ==Q3.STATE_QUOTES; */
if(currState == Q3.STATE_NORMAL)
currState = Q3.STATE_SLASH;
else if(currState == Q3.STATE_COMMENT)
currState = Q3.STATE_NORMAL;
break;
case '*':
if(currState == Q3.STATE_SLASH)
currState = Q3.STATE_COMMENT;
break;
case '"':
if(currState == Q3.STATE_NORMAL)
currState = Q3.STATE_QUOTE_DOUBLE;
if(currState == Q3.STATE_QUOTE_DOUBLE)
currState = Q3.STATE_NORMAL;
break;
*case ''':
if(currState == Q3.STATE_NORMAL)
currState = Q3.STATE_QUOTE_SINGLE;
if(currState == Q3.STATE_QUOTE_SINGLE)
currState = Q3.STATE_NORMAL;
break;*
}
if(currState != Q3.STATE_COMMENT)
System.out.print(outChar);
}
}
}
我可能是錯的,但這看起來像[正則表達式]的工作(http://download.oracle.com/javase/tutorial/essential/regex/) – mre