如何将这个java代码打印到控制台没有错误?

时间:2015-01-17 22:53:53

标签: java string

public static void stripComments(Scanner input) {
    System.out.println("import java.util.*;");
    System.out.println();
    System.out.println("public class Program {");
    System.out.println("    public static void main(String[] args) {");
    System.out.println("      System.out.println(\"Hello, world!\");");
    System.out.println("    }");
    System.out.println();
    System.out.println("    public static  void foo() {");
    System.out.println("        System.out.println(\"Goodbye!\");");
    System.out.println("    }");
    System.out.println("}");
}

我正在尝试将此打印到控制台而没有错误,但它一直给我一个错误:

Line 5
You may have forgotten to include a closing " mark to end a string.
unclosed string literal
    System.out.println("
                       ^
Line 5
You may have forgotten to end a statement with a semicolon. Each Java statement must end with a semicolon.
';' expected
    System.out.println("
                        ^
Line 6
This is a very general syntax error that may be caused by a variety of problems, such as misplaced { } braces or an incorrect sequence of Java keywords/operators. Sometimes the error is not on the line indicated but rather on a previous line.
illegal start of expression
    System.out.println();
          ^
Line 6
You may have forgotten to end a statement with a semicolon. Each Java statement must end with a semicolon.
';' expected
    System.out.println();
              ^
4 errors

它可能需要对转义序列做一些事情,但我似乎无法放置或放置它们。

2 个答案:

答案 0 :(得分:3)

  

System.out.println(" System.out.println('' Hello,world!'');");

我猜这是第7行。

在java中你使用" \"作为逃脱角色。所以你的代码应该是:

System.out.println("      System.out.println(\"Hello, world!\");");

答案 1 :(得分:-3)

public class Parallel
{   
  public static void main( String[] args )
  { 


System.out.println("import java.util.*;\n\n\npublic class Program {\n\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(''Hello, world!'');\n\t }\n\t public static  void foo() {\n\t\tSystem.out.println(''Goodbye!'');\n\t}\n}");


    }
}

我的项目名为Parallel ready,所以我只是在那里做了

当涉及到“你需要使用\ n作为nee line而\ t \ t为tab”的打印行时,eclipse不会将ur字母与ur命令混合起来相当容易。