刚刚做一个很简单的练习,写了以下代码:import java.util.*;public class Average 
{
        int firstNo,secondNo,thirdNo,averageNo;
Scanner keyboard=new Scanner(System.in); public static void main(String[] args) 
{
System.out.println("Enter three numbers seperated by one or more spaces:");
                firstNo=keyboard.nextInt();
secondNo=keyboard.nextInt();
thirdNo=keyboard.nextInt(); averageNo=(firstNo+secondNo+thirdNo)/3;
System.out.println("The average number is "+averageNo);
}
}编译不通过,好像是说我没有定义firstNo/secondNo/thirdNo这三个变量,而当我把代码改成:
import java.util.*;public class Average 
{ public static void main(String[] args) 
{
int firstNo,secondNo,thirdNo,averageNo;
        Scanner keyboard=new Scanner(System.in); System.out.println("Enter three numbers seperated by one or more spaces:");
                firstNo=keyboard.nextInt();
secondNo=keyboard.nextInt();
thirdNo=keyboard.nextInt(); averageNo=(firstNo+secondNo+thirdNo)/3;
System.out.println("The average number is "+averageNo);
}
}时,上面的问题就解决了。为什么我不能在方法外定义变量?一定要在方法里面定义变量吗?请高手解答我的疑惑。