[英]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.