从switch语句java打印出来

时间:2015-01-26 21:26:57

标签: java

这是一段代码:

class Main {
public static void main(String[] args) {
    try {
        CLI.parse (args, new String[0]);

        InputStream inputStream = args.length == 0 ?
                System.in : new java.io.FileInputStream(CLI.infile);
        ANTLRInputStream antlrIOS = new ANTLRInputStream(inputStream);

        if (CLI.target == CLI.SCAN || CLI.target == CLI.DEFAULT)
        {
            DecafScanner lexer = new DecafScanner(antlrIOS);
            Token token;
            boolean done = false;
            while (!done)
            {
                try
                {
                    for (token=lexer.nextToken();
                        token.getType()!=Token.EOF; token=lexer.nextToken())
                    {
                        String type = "";
                        String text = token.getText();


                        switch (token.getType())
                        {
                        case DecafScanner.ID:
                          type = " CHARLITERAL";
                          break;

                        }
                        System.out.println (token.getLine() + type + " " + text);
                    }
                    done = true;
                } catch(Exception e) {
                    // print the error:
                    System.out.println(CLI.infile+" "+e);
                }
            }
        }
        else if (CLI.target == CLI.PARSE)
        {
            DecafScanner lexer = new DecafScanner(antlrIOS);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            DecafParser parser = new DecafParser (tokens);
            parser.program();
        }

    } catch(Exception e) {
        // print the error:
        System.out.println(CLI.infile+" "+e);
    }
}
}

它按原样打印出来但不知何故它不打印出类型的默认值,即空字符串。如何从switch语句中打印出来?

谢谢!

1 个答案:

答案 0 :(得分:0)

  1. 尝试调试。
  2. 尝试从开关部分打印值,看看你是否进入过它。
  3. 尝试用简单的" =="替换开关。看看你是否得到了#34; token.getType()== DecafScanner.ID"
  4. 一般建议 - 移动"类型"的定义和" next"在循环之外,以避免一次又一次地重新创建它们。