Java SE做while循环问题

Java SE做while循环问题,第1张

Java SE做while循环问题

您正在尝试访问声明范围之外的变量。您必须在声明外部声明变量,

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
。我怀疑您要检查。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5430982.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存