下面的代码应该让用户输入建筑物的平方英尺和故事。我一直在主要和我的生活中的代码存在问题,我无法弄清楚为什么。java:非静态变量不能从静态上下文中引用错误
import java.util.*;
public class Building
{
static Scanner console = new Scanner(System.in);
double area, squarefootage;
int floors, stories;
char ans
void get_squarefootage()
{
System.out.println ("Please enter the square footage of the floor.");
area = console.nextDouble();
}
void set_squarefootage(double area)
{
squarefootage = area;
}
void get_stories()
{
System.out.println ("Please enter the number of floors in the building.");
floors = console.nextInt();
}
void set_stories(int floors)
{
stories = floors;
}
void get_info()
{
System.out.println (" The area is: " + squarefootage + " feet squared");
System.out.println (" The number of stroies in the building: " + stories + " levels");
}
public static void main(String[] args)
{
Building b = new Building();
b.get_squarefootage();
b.set_squarefootage(area);
b.get_stories();
b.set_stories(floors);
System.out.println ("---------------");
b.get_info();
System.out.println ("Would you like to repeat this program? (Y/N)");
}
}
问题我有48和50行,它显示错误;
Building.java:48: error: non-static variable area cannot be referenced from a static context
b.set_squarefootage(area);
^
Building.java:50: error: non-static variable floors cannot be referenced from a static context
b.set_stories(floors);
感谢您的帮助
将你的头转向显示器的右侧,看看'Related'部分。 –
在右侧的“相关”下查看。从静态上下文中引用的非静态事物有多少个问题?其中之一肯定会对你有一个很好的答案。 –
当你在第48行使用'area'时,你期望它来自哪里? –