2014-07-08 40 views
1

我有這個應用程序,其中有一個數組元素4.我需要將每個單個房間對象添加到數組中,當用戶輸入時,他/她最多可以輸入4個對象。但是當我只輸入一個房間對象時,它顯示了該對象添加的數組中的所有元素。我可以知道爲什麼,有人可以幫忙嗎? 謝謝!爲什麼我的for循環同時添加一個對象?

import java.util.Arrays; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 

class TestRoom { 

public static void main(String [] args) 
{ 
    String[] roomsInHouse = new String[4]; 
    int answer; 

    do{ 
    String nameOfRoom = JOptionPane.showInputDialog(null, "Enter the name of the room"); 
    String dimsOfRoom = JOptionPane.showInputDialog(null, "Enter the dimension of the room in feet L X W X H"); 

    Scanner userInput = new Scanner(dimsOfRoom); 
    double l = userInput.nextDouble(); 
    double w = userInput.nextDouble(); 
    double h = userInput.nextDouble(); 

    Room roomDetails = new Room(nameOfRoom, l, w, h); 

    String n = Double.toString(l); 
    String o = Double.toString(w); 
    String p = Double.toString(h); 

    for(int i = 0; i < roomsInHouse.length; i++) 
    { 
     roomsInHouse[i] = nameOfRoom + dimsOfRoom; 
    } 

    answer = JOptionPane.showConfirmDialog(null, "Input another room?", "Area/Volume calculator", JOptionPane.YES_NO_CANCEL_OPTION); 

    } while(answer == JOptionPane.YES_OPTION); 

    for(String j: roomsInHouse) 
    { 
     System.out.println(j); 
    } 
} 

} 

好的我剛剛改變了我的編碼,並感謝你們的貢獻,我明白出了什麼問題。乾杯!

int counter = 0; 

roomsInHouse[counter++] = nameOfRoom + dimsOfRoom; 

answer = JOptionPane.showConfirmDialog(null, "Input another room?", "Area/Volume calculator", JOptionPane.YES_NO_CANCEL_OPTION); 

} while(answer == JOptionPane.YES_OPTION && counter < 4); 
+4

「但是,當我只輸入一個房間對象時,它顯示了該對象添加的數組中的所有元素。」不正是嵌套的'for'循環正在做什麼? – dasblinkenlight

+0

你確定你正在輸入房間的名字嗎?因爲我沒有在代碼中看到它 – gprathour

回答

1

就在這裏,你正在用一個房間填滿整個陣列。 roomsInHouse.length總是返回4

for(int i = 0; i < roomsInHouse.length; i++) 
{ 
    roomsInHouse[i] = nameOfRoom + dimsOfRoom; 
} 

你可以改變你的代碼,因此

String[] roomsInHouse = new String[4]; 
int n = 0; 
int answer; 

再後來:

roomsInHouse[n++] = nameOfRoom + dimsOfRoom; 

之後,只是檢查是否n < 4,這意味着更多的房間還可以添加。

1

如果我正確地回答您的問題,您說當用戶輸入一個房間時,您的數組已滿。如果是這種情況,我認爲。

for(int i = 0; i < roomsInHouse.length; i++) 
    { 
    roomsInHouse[i] = nameOfRoom + dimsOfRoom; 
    } 

是你的問題,因爲它遍歷所有的數組。

1

錯就錯在你與在do-while循環......

爲u的迭代「roomsInHouse.lenght」次do-while循環的每一個互爲作用環.. ...則數組被改寫爲每個互爲作用....

執行以下更正你的代碼....

import java.util.Arrays; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 

class TestRoom { 

public static void main(String [] args) 
{ 
String[] roomsInHouse = new String[4]; 
int answer; 
int counter=0; 
do{ 
String nameOfRoom = JOptionPane.showInputDialog(null, "Enter the name of the room"); 
String dimsOfRoom = JOptionPane.showInputDialog(null, "Enter the dimension of the room in feet L X W X H"); 

Scanner userInput = new Scanner(dimsOfRoom); 
double l = userInput.nextDouble(); 
double w = userInput.nextDouble(); 
double h = userInput.nextDouble(); 

Room roomDetails = new Room(nameOfRoom, l, w, h); 

String n = Double.toString(l); 
String o = Double.toString(w); 
String p = Double.toString(h); 


roomsInHouse[counter] = nameOfRoom + dimsOfRoom; 

counter++; 
answer = JOptionPane.showConfirmDialog(null, "Input another room?", "Area/Volume calculator", JOptionPane.YES_NO_CANCEL_OPTION); 

} while(answer == JOptionPane.YES_OPTION); 

for(String j: roomsInHouse) 
{ 
    System.out.println(j); 
} 

}

}

+0

第四次迭代後會發生什麼? –

+0

你在問關於循環.... ???? – Jaithera

+0

我在問,如果用戶在陣列滿了之後仍然輸入yes來輸入更多的房間?您應該在while語句中添加另一個條件 –

相關問題