gpt4 book ai didi

Java SE do while 循环问题

转载 作者:行者123 更新时间:2023-12-01 18:30:24 30 4
gpt4 key购买 nike

当我编译代码时,我的 java 应用程序出现问题,它说找不到符号 roomLength。应用程序应该做的是提示用户输入房间名称,然后如果它是“退出”,那么它必须关闭程序。否则会提示房间的长、宽、高。如果这些维度中的任何一个为零或小于零,它将再次重新提示输入维度。

class Room
{
private String name;
private double length;
private double width;
private double height;

public Room(String r, double l, double w, double h)
{
name = r;
length = l;
width = w;
height = h;
}


public String getName()
{
return name;
}

public double getLength()
{
return length;
}

public double getWidth()
{
return width;
}

public double getHeight()
{
return height;
}

public double getArea()
{
double area = length*width;
return area;
}

public double getVolume()
{
double volume = length*width*height;
return volume;
}

public void setName(String s)
{
name = s;
}
}

这是我的主要方法。

import java.util.Scanner;

class TestRoom
{
public static void main(String [] args)
{
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter the name of room");

String roomName = userInput.next();
if(roomName.equals("quit"))
{
userInput.close();
} else
{
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();

Room r = new Room(roomName, roomLength, roomWidth, roomHeight);
}
while (roomLength > 0 || roomWidth > 0 || roomHeight > 0);


System.out.println("The name of room: " + r.getName() + " Dimensions: L= " + r.getLength() +
"X " + "W= " + r.getWidth() + "X " + "H= " + r.getHeight());
System.out.println("Area of room= " + r.getArea());
System.out.println("Volume of room= " + r.getVolume());

}
}
}

提前致谢! ;)

还有另一个问题几乎与这个问题类似,只不过它应该循环回到第一个提示,允许用户输入另一个房间的名称及其尺寸。

Java do while loop back to the beginning of application

干杯!

最佳答案

您正在尝试访问声明范围之外的变量。您必须在 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

关于Java SE do while 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474905/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com