2013-04-15 34 views
-5

嘿,我是在Java初学者和正在错误在第11行,我不知道为什么我收到此错误下面的代码的System.out.println错误

package practice; 
import java.util.Scanner; 

public class VariablesDemo { 

int empid; 
char gender; 
float allowance; 
double basicSalary; 
Scanner src = new Scanner(System.in); 
System.out.println("Enter the Employee id"); 
public void setEmployeeId(int empid) 
{ 
    System.out.println("Enter the Employee id"); 
    this.empid= src.nextInt(); 
} 
public void setEmployeeGender(char gender) 
{ 
    System.out.println("Enter the Employee gender"); 
    String gender_type=src.next(); 
    this.gender= gender_type.charAt(0); 
} 
public void setEmployeeAllowance(float allowance) 
{ 
    System.out.println("Enter the Employee allowance"); 
    this.allowance= src.nextFloat(); 
} 
public void setEmployeeBasicSalary(double basicSalary) 
{ 
    System.out.println("Enter the Employee basic Salary"); 
    this.basicSalary= src.nextDouble(); 
} 
} 
+1

有什么错误? – mre

+0

完全删除第11行,你已经在'setEmployeeId'方法中使用它了。 – maksimov

+2

这是错误的,这不仅是像你val这样的专家的地方,而且对于初学者也是,你有什么权利要求他解决他的问题! – anshulkatta

回答

2
System.out.println("Enter the Employee id"); 

应该是里面任何方法。

如果你还想要写SOP的方法 之外,那么请参阅本Question

1
System.out.println("Enter the Employee id"); 

这条线是用于打印此可执行行应在方法只

只有初始化和声明中写入允许外部方法

1

执行语句必须在方法内。

System.out.println("Enter the Employee id"); 

上面一行应该在方法中。不在课堂上直接。

只有变量初始化,声明,静态块,init块在方法外才允许。我会建议先阅读基本的Java教程,然后再尝试使用java编码。

2

System.out.println()应的任何方法,因为它的一个方法调用....

Scanner src=new Scanner(System.in);可以在任何地方上课的方法.. !!

0

System.out.println("Enter the Employee id");

是方法println()调用,因此它不能是变量声明中...

2

作为初学者,你应该首先尝试掌握语言的基本知识,如如何定义类,属性或方法。使用参考书或一个很好的教程开始。

祝你好运!