Java SE执行while循环问题

时间:2014-06-29 09:46:29

标签: java loops while-loop do-while

当我编译代码时,我的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

干杯!

2 个答案:

答案 0 :(得分:2)

您必须在roomLength循环前面声明变量roomWidthroomHeightdo-while

像这样:

double roomLength = 0;
double roomWidth = 0;
double roomHeight = 0;
do { 
    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);

问题是变量的范围。 roomLengthroomWidthroomHeight仅在do - 块中可见。但是while内的语句不在do - 块之内。这就是无法找到变量的原因。

答案 1 :(得分:2)

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