英文:
Trying to iterate through a list
问题
我正在尝试创建一个迭代列表,其中将文本设置为"Copper 60 = Silver 30"。因此,"Copper"和"Silver"是字符串,然后60来自于editText,而30来自于转换后的成本。目前它显示的是 "Copper 30 = Silver",因此显示方式错误,并且没有添加来自ediText的60。有人可以帮忙吗?我已经被卡住好几个小时了。以下是我的代码:
myList.add(convertedCost.getText().toString());
editText.getText();
editText.setText("");
String copperStr = "Copper";
String silverStr = "Silver";
for(String editText : myList){
copperStr = copperStr + " " + editText + " = " + silverStr;
}
txtList.setText(copperStr);
}
});
英文:
Im trying to make a iterating list where it will set the text to "Copper 60 = Silver 30" So the "Copper" and "Silver" are strings then the 60 comes from the editText and the 30 comes from the converted cost. At the moment it is displaying
"Copper 30 = Silver" So it is displaying it the wrong way and not adding in the 60 from the ediText. Can someone please help ive been stuck on it for hours. Here is my code:
myList.add(convertedCost.getText().toString());
editText.getText();
editText.setText("");
String copperStr = "Copper";
String silverStr = "Silver";
for(String editText : myList){
copperStr = copperStr + " " + editText + " = " + silverStr;
}
txtList.setText(copperStr);
}
});
答案1
得分: 1
问题在于,在进入 for 循环时,myList 只包含一个单独的元素:30,来自于 convertedCost.getText().ToString()。在任何时候都没有将铜价值60添加到 myList 中。
当你通过 for 循环时,editText 被赋值为 myList 中的下一个元素,从第一个开始。myList 中只有一个元素,即30,这意味着在第一个循环中,变量如下所示:
myList 是 ["30"]
editText 是 myList[0] 是 "30"
copperStr 是 "Copper"
silverStr 是 "Silver"
因此,当你完成第一个循环时,copperStr 被赋值为以下内容:
copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper" + " " + "30" + " = " + "Silver"
copperStr = "Copper 30 = Silver"
但是,如果你在插入 "30" 之前,将 "60" 插入列表,那么第一个循环将设置以下变量
myList 是 ["60", "30"]
editText 是 myList[0] 是 "60"
copperStr 是 "Copper"
silverStr 是 "Silver"
第一个循环将产生以下结果:
copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper" + " " + "60" + " = " + "Silver"
copperStr = "Copper 60 = Silver"
然后在第二个循环中,变量将如下所示:
myList 是 ["60", "30"]
editText 是 myList[1] 是 "30"
copperStr 是 "Copper 60 = Silver"
silverStr 是 "Silver"
第二个循环将产生以下结果:
copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper 60 = Silver" + " " + "30" + " = " + "Silver"
copperStr = "Copper 60 = Silver 30 Silver"
这也不是你想要的。
相反,你可能希望类似于以下的操作
myList.add("60");
myList.add(convertedCost.getText().toString());
editText.getText();
editText.setText("");
String copperStr = "Copper";
String silverStr = "Silver";
editText.setText(copperStr + " " + myList[0] + " = " + myList[1] + " " + silverStr);
txtList.setText(editText);
英文:
The main problem with this is that myList, by the time you get to the for loop, only contains a single element: 30, from the convertedCost.getText().ToString(). At no point is the 60 for the copper value added to myList.
When you go through the for loop, editText is assigned to the next element in myList, starting with the first. The only element in myList, being 30, means that in the first loop, the variables are as follows:
myList is ["30"]
editText is myList[0] is "30"
copperStr is "Copper"
silverStr is "Silver"
So that when you complete your first loop, the following is assigned to copperStr:
copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper" + " " + "30" + " = " + "Silver"
copperStr = "Copper 30 = Silver"
But if you inserted "60" into the list, before you inserted "30", then the first loop would have the following variables set
myList is ["60", "30"]
editText is myList[0] is "60"
copperStr is "Copper"
silverStr is "Silver"
And the first loop would produce the following:
copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper" + " " + "60 + " = " + "Silver"
copperStr = "Copper 60 = Silver"
Then by the second loop, the variables would be as follows:
myList is ["60", "30"]
editText is myList[1] is "30"
copperStr is "Copper 60 is Silver"
silverStr is "Silver"
And the second loop would produce the following result:
copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper 60 = Silver" + " " + "30" + " = " + "Silver"
copperStr = "Copper 60 = Silver 30 Silver"
Which is also not what you want
You may instead wish for something akin to the following
myList.add("60");
myList.add(convertedCost.getText().toString());
editText.getText();
editText.setText("");
String copperStr = "Copper";
String silverStr = "Silver";
editText.setText(copperStr + " " + myList[0] + " = " + myList[1] + " " + silverStr);
txtList.setText(editText);
答案2
得分: 0
首先,您正在重用变量名editText
,因此您失去了对它的访问权限。但这并不重要,因为无论如何您都将其设置为空字符串。
其次,根据您的代码,editText
和 convertedCost
实际上是相同的,因为您将 convertedCost
添加到 myList
,然后从该列表中读取作为 editText
。
第三,您没有获取后面的数字 60,因为您没有将它连接到字符串中。
也许这段代码可以帮助您:
copperStr = String.format("Copper %s = Silver %s", editText, convertedCost.getText().toString());
这有点类似于使用 +
连接多个字符串,但以一种更易读的方式进行操作。
英文:
First, you are reusing variable name editText
, so you are losing access to it. But that doesn't matter since you are setting it to an empty string anyway.
Second, according to your code, editText
and convertedCost
are the exact same thing, since you are adding the convertedCost
to the myList
and then reading from that list as the editText
.
Third, you are not getting the latter number 60 because you are not concatenating it to the string.
May be this code will help you out:
copperStr = String.format("Copper %s = Silver %s", editText, convertedCost.getText().toString());
This is kinda like concatenating several strings with +
but doing it in a more readable fashion
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论