您正在尝试访问在声明范围之外的变量。您必须在声明外部声明变量,
do以便可以在中访问它们
while:
double roomLength; double roomWidth; double roomHeight; do { Scanner dimensionsInput = new Scanner(System.in); System.out.print("Please enter the dimensions of the room, length, width and height accordingly"); roomLength = dimensionsInput.nextDouble(); roomWidth = dimensionsInput.nextDouble(); roomHeight = dimensionsInput.nextDouble(); Room r = new Room(roomName, roomLength, roomWidth, roomHeight); } while (roomLength > 0 || roomWidth > 0 || roomHeight > 0);
但是我现在看到这
Room也是用错误的范围声明的。如果要随后访问它,则必须在循环之前声明它。因此,一个更简单的解决方案可能是:
Room r; do { Scanner dimensionsInput = new Scanner(System.in); System.out.print("Please enter the dimensions of the room, length, width and height accordingly"); double roomLength = dimensionsInput.nextDouble(); double roomWidth = dimensionsInput.nextDouble(); double roomHeight = dimensionsInput.nextDouble(); r = new Room(roomName, roomLength, roomWidth, roomHeight); } while (r.getLength() > 0 || r.getWidth() > 0 || r.getHeight() > 0);
顺便说一句,直到所有尺寸都为0,才循环播放似乎不对
== 0。我怀疑您要检查。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)