当我编译代码时,我的Java应用程序出现问题,它说它无法找到符号roomLength。 应用程序应该做的是提示用户输入房间名称,然后如果它“退出”,则必须关闭程序。否则会提示房间的长度,宽度和高度。如果这些维度中的任何一个为零或更小,它将再次重新提示维度。Java SE在循环问题时执行
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
干杯!
感谢我设法让它现在工作,由于你们的帮助和解释。我不知道你可以实例化一个对象在拆分的形式。 –
好吧,现在我有另一个新的问题。如何循环回应用程序的开始?我的意思是在获得用户的所有预期输入之后。我如何将应用程序循环回到提示用户再次访问其他房间属性的位置?谢谢! –
在SO上的简短搜索导致:[link](http://stackoverflow.com/questions/21215914/how-to-repeat-something-until-correct-input-is-given-in-java/21215984# 21215984)如果你有一个具体的问题/问题,它更容易创建一个新的问题。 – Tarlen