Java-教程中出现语法错误
我有以下代码:Java-教程中出现语法错误,java,error-handling,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
/* 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");
}}
/*演示if。
将此文件称为IfDemo.java*/
包ifdemo;
公共静态void main(字符串args[}){
INTA、b、c{
a=2;
b=3;
如果(a尝试固定支架:
。。。
公共静态void main(字符串参数[])
…您编写的是}
而不是]
公共静态void main(字符串args[]){
类丢失,在int a、b、c后面;您有大括号…您必须删除它,字符串应该是(字符串[]args)在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;
公共类IfDemo{/应该是String args[]
,而不是String args[}
。或者更好的是:String[]args
,这更清楚地表明args
是字符串数组类型的变量
主方法应该在名为IfDemo
的类中。不能只在类之外声明方法
另外,Java区分大小写C
和C
不是一回事。System
和System
不是一回事。您的方法应该在类中。您的文件名表明它应该在类中
public class IFDemo {
我建议您使用IDE来帮助您编写代码。这将确保您不会因为基本代码段丢失/不正确而陷入困境。publicstaticvoidmain(stringargs[]){//Its[]而不是[}
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");
}
INTA、b、c;
a=2;
b=3;
如果(a=0)//它不是大写字母C,而是小写字母C
System.out.println(“C是非负的”);//它的大写字母S,而不是小写字母S
if(c<0)
System.out.println(“C为负数”);
System.out.println();
c=b-a;//c包含1//其资本
System.out.println(“C包含1”);
如果(c>=0)System.out.println(“c为非负”);
如果(csupristed)您错过了缺少的类,那么会有很多错误。请注意Java中变量的情况。如果(c>=0),则为
等。也将代码封装在类中。请单击并在更新后发布修改后的代码。事实上,这部分很好。没用,但很好。谢谢。我编辑了代码,但仍然有一个错误:“公共类型ifdemo必须在其自己的文件中定义”@mm1985请编辑问题并发布更新的代码,最好是在单独的代码块中。单击问题下的“编辑”链接进入编辑模式。@mm1985“公共类型ifdemo必须在其自己的文件中定义”表示您的文件名不是“ifdemo.java”。请确保文件名和类名正确match@mm1985您需要调用文件IfDemo.java