为什么我的for循环同时添加一个对象?

时间:2014-07-08 16:08:35

标签: java loops for-loop

我有这个应用程序,其数组元素为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);

3 个答案:

答案 0 :(得分:1)

如果我对你提出正确的问题,你就是说当用户输入一个房间时你的阵列就会变满。如果是这种情况,我认为。

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

是你的问题,因为它循环遍历所有数组。

答案 1 :(得分: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,这意味着可以添加更多房间。

答案 2 :(得分:1)

错误在你的for循环中,在do-while循环......

当你为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);
}

}

}