2014-06-29 130 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! –