【问题标题】:Java - syntax error during tutorialJava - 教程期间出现语法错误
【发布时间】:2013-07-20 05:10:23
【问题描述】:

我有以下代码:

 /* Demonstrate the if.
Call this file IfDemo.java. */

package ifdemo;

public static void main(String args[}) {
    int a,b,c;{
    a=2;
    b=3;

    if (a<b) System.out.println("A is less than B");

    //this won't display anything if (a==b) System.out.println("You won't see this")
    System.out.println();
    c=a-b; // C contains -1
    System.out.println("C contains -1");
    if (C >= 0) system.out.println("C is non-negative");
    if (C < 0) system.out.println("C is negative");

    System.out.println();
    C=b-a; //C contains 1 
    System.out.println("C contains 1");
    if (C >=0)System.out.println("C is non-negative");
    if (C<0)System.out.println("C is negative");

}}

在线: public static void main(String args[} ) { 我得到三个错误: 1.令牌“void”的语法错误,@预期 2.令牌上的语法错误,应改为类头 3.标记的语法错误,错位的构造

希望大家能帮帮我。

提前致谢。

【问题讨论】:

  • 注意Java中变量的大小写。是if (c &gt;= 0)等。同样把代码封装在一个类中
  • 请在更新后点击edit并发布您修改后的代码。

标签: java error-handling


【解决方案1】:

在 Java 中没有独立的函数,您在 main 函数之外缺少一个类声明。以下是代码结构的外观:

package ifdemo;

public class IfDemo { // <<== You are missing this line

    public static void main(String args[]) { // <<== You have a typo here
        .... //                         ^
        .... //           This should be a square bracket
    }

}

还要注意代码中的“杂散”花括号:保持花括号平衡非常重要,否则程序将无法编译并出现非常奇怪的错误。

【讨论】:

  • 好的,谢谢。我编辑了代码,但仍然有一个错误:“公共类型 ifdemo 必须在其自己的文件中定义”
  • @mm1985 请编辑问题并发布您更新的代码,最好是在单独的代码块中。点击问题下方的“编辑”链接进入编辑模式。
  • @mm1985 "公共类型ifdemo必须定义在自己的文件中" 表示你的文件名不是"ifdemo.java"。确保文件名和类名匹配
  • @mm1985 你需要调用你的文件IfDemo.java
【解决方案2】:

尝试固定支架:

... 公共静态无效主(字符串参数 []) ...

【讨论】:

    【解决方案3】:

    你写的是}而不是]

    public static void main(String args[]) {

    【讨论】:

      【解决方案4】:

      类丢失,在 int a,b,c 后面;你有大括号......你必须删除它,字符串应该是 (String[]args)

      【讨论】:

      • 其实这部分没问题。没用,但很好。
      【解决方案5】:

      你的方法应该在一个类中。您的文件名表明它应该是

      public class IFDemo {
      

      我建议您使用 IDE 来帮助您编写代码。这将确保您不会因为缺少/不正确的基本代码片段而走得太远。

      【讨论】:

        【解决方案6】:
        public static void main(String args[]) {   //Its [] and not [}
        
                int a,b,c;
        
                a=2;
                b=3;
        
                if (a<b) System.out.println("A is less than B");
        
        
                System.out.println();
                c=a-b; // C contains -1
                System.out.println("C contains -1");
                if (c >= 0)    // Its not capital C . Its small c
                    System.out.println("C is non-negative");   // Its capital S and not small s
                if (c < 0)
                      System.out.println("C is negative");
        
                System.out.println();
                c=b-a; //C contains 1   // Its Capital 
                System.out.println("C contains 1");
                if (c >=0)System.out.println("C is non-negative");
                if (c<0)System.out.println("C is negative");
        
            }
        

        【讨论】:

          【解决方案7】:

          应该是String args[] 而不是String args[}。甚至更好:String[] args,这更清楚地表明args 是字符串数组类型的变量。

          主要方法应该在一个名为IfDemo的类中。您不能只在类之外声明方法。

          另外,Java 区分大小写 Cc 不是一回事。 Systemsystem 不是一回事。

          【讨论】:

          • 很惊讶你错过了缺课。 ;) 好吧,有很多错误。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-09
          • 1970-01-01
          • 2019-02-07
          • 1970-01-01
          • 1970-01-01
          • 2011-10-01
          • 1970-01-01
          相关资源
          最近更新 更多