Java - 教程期间出现语法错误

标签 java error-handling

我有以下代码:

 /* 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 中没有独立的函数,您在 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
    }

}

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

关于Java - 教程期间出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17771101/

相关文章:

java - Guava Hasher 有时对同一对象给出不同的结果

java - jackson 在 map 上捕捉到无法识别的领域

r - 无法在 R 中加载任何包(无法加载共享对象)

iis - asp.net以斜杠结尾的无效路径不会返回错误页面IIS

vba - VBA-Countif错误 '438'

java - 如何让优先级队列自行重新排序

java - YAML 自定义对象

java - 无法在 BI PUBLISHER 中加载架构信息

error-handling - 绕过Play的HttpErrorHandler处理4xx错误

python - 如何在 try except block 中检查某种类型的 OSError?