【发布时间】:2014-02-27 22:44:51
【问题描述】:
以下代码应让用户输入建筑物的平方英尺和楼层。我一直遇到 main 中的代码问题,而且我一生都无法弄清楚原因。
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时,你期望它来自哪里?