2014-06-29 84 views
0

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

干杯!

回答

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

+0

感谢我设法让它现在工作,由于你们的帮助和解释。我不知道你可以实例化一个对象在拆分的形式。 –

+0

好吧,现在我有另一个新的问题。如何循环回应用程序的开始?我的意思是在获得用户的所有预期输入之后。我如何将应用程序循环回到提示用户再次访问其他房间属性的位置?谢谢! –

+0

在SO上的简短搜索导致:[link](http://stackoverflow.com/questions/21215914/how-to-repeat-something-until-correct-input-is-given-in-java/21215984# 21215984)如果你有一个具体的问题/问题,它更容易创建一个新的问题。 – Tarlen

2

您必须在do-while循环前声明变量roomLength,roomWidthroomHeight

像这样:

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); 

的问题是你的变量的作用域。 roomLength,roomWidthroomHeight只在do -block内部可见。但是while中的语句不在do-区块之内。这就是为什么变量无法找到。 。

+0

thansk你的帮助Tarlen! –