数组更改元素的问题
Problems with array changing elements
我正在尝试使用 Jbutton 将数组中的元素更改为单词 "empty",并且如果数组中选定位置的 为空,则还通过 Jtextfield 添加名称。出于某种原因,我无法让它工作。这是代码不知道我是否遗漏了什么或者我完全错了
move = new JButton("Add Tennant");
window.add(move);
moveIn.addActionListener(this);
Tennant = new JTextField(FIELD_WIDTH);
nTennant.setText("Enter new name") ;
window.add(Tennant);
Tennant.addActionListener(this);
evict = new JButton("Evict");
window.add(evict);
moveIn.addActionListener(this);
不同的方法:
if(e.getSource() == move)
{
if (occupant[selectedApartment].equals("empty"))
{
occupant[selectedApartment] = Tennant.getText();
}
}
if(e.getSource() == evict)
{
if(!occupant[selectedApartment].equals("Empty"))
{
occupant[selectedApartment] = "Empty";
}
}
首先让我吃惊的是,您使用 occupant[selectedApartment] = "Empty";
设置公寓为空,但使用 if (occupant[selectedApartment].equals("empty"))
测试公寓是否为空
"Empty" != "empty"
你可以改变
if (occupant[selectedApartment].equals("empty"))
到
if (occupant[selectedApartment].equals("Empty"))
或使用
if (occupant[selectedApartment].equalsIgnoreCase("empty"))
或更改
occupant[selectedApartment] = "Empty";
到
occupant[selectedApartment] = "empty";
我正在尝试使用 Jbutton 将数组中的元素更改为单词 "empty",并且如果数组中选定位置的 为空,则还通过 Jtextfield 添加名称。出于某种原因,我无法让它工作。这是代码不知道我是否遗漏了什么或者我完全错了
move = new JButton("Add Tennant");
window.add(move);
moveIn.addActionListener(this);
Tennant = new JTextField(FIELD_WIDTH);
nTennant.setText("Enter new name") ;
window.add(Tennant);
Tennant.addActionListener(this);
evict = new JButton("Evict");
window.add(evict);
moveIn.addActionListener(this);
不同的方法:
if(e.getSource() == move)
{
if (occupant[selectedApartment].equals("empty"))
{
occupant[selectedApartment] = Tennant.getText();
}
}
if(e.getSource() == evict)
{
if(!occupant[selectedApartment].equals("Empty"))
{
occupant[selectedApartment] = "Empty";
}
}
首先让我吃惊的是,您使用 occupant[selectedApartment] = "Empty";
设置公寓为空,但使用 if (occupant[selectedApartment].equals("empty"))
测试公寓是否为空
"Empty" != "empty"
你可以改变
if (occupant[selectedApartment].equals("empty"))
到
if (occupant[selectedApartment].equals("Empty"))
或使用
if (occupant[selectedApartment].equalsIgnoreCase("empty"))
或更改
occupant[selectedApartment] = "Empty";
到
occupant[selectedApartment] = "empty";