我有以下代码:
/* 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/