[英]Why does my for loop adds an object simultaneously?
我有這個應用程序,其數組元素為4.我需要在用戶輸入時將每個Room對象添加到數組中,並且他/她最多可以輸入4個對象。 但是當我只輸入一個Room對象時,它會顯示對象添加的數組中的所有元素。 我可以知道為什么,有人可以幫忙嗎? 謝謝!
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);
如果我對你提出正確的問題,你就是說當用戶只輸入一個房間時你的陣列就會變滿。 如果是這種情況我認為。
for(int i = 0; i < roomsInHouse.length; i++)
{
roomsInHouse[i] = nameOfRoom + dimsOfRoom;
}
是你的問題,因為它循環遍歷所有數組。
就在這里,您將用一個房間填滿整個陣列。 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
,這意味着可以添加更多房間。
在do-while循環中你的for循環錯誤......
因為你正在為do-while循環的每次交互迭代“roomsInHouse.lenght”時間......數組被重寫為每次交互....
對您的代碼進行以下更正....
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.