我有一个API,它有一个for循环并打印出接下来3天的天气状况。
for (ForecastForday1 day : forecast)
{
// Print out what day the forecast is for, and
// the conditions on that day
System.out.println("The weather on " + day.getDayOfWeek()
+ " will be " + day.getInfo("Conditions"));
}
但我有3个JTextArea所以每次我想要循环重复它将数据放在一个文本区域,然后是下一个。
我的文字区域:
day1.append("");
day2.append("");
day3.append("");
所以我想我必须在这个循环上设置循环,但不知道从哪里开始。
答案 0 :(得分:3)
如果你可以使用textareas数组而不是3个不同的变量,你可以做类似的事情
JTextArea days[] = new JTextArea[3];
int i=0;
for (ForecastForday1 day : forecast) {
days[i++].append("Append string");
}
答案 1 :(得分:1)
我可以通过多种方式提出一种方法来解决这个问题,
int i=0;
for (ForecastForday1 day : forecast)
{
if (i%3==0)
day1.append("string here");
else if (i%3==1)
day2.append("string here");
else if (i%3==2)
day3.append("string here");
i++;
// Print out what day the forecast is for, and
// the conditions on that day
System.out.println("The weather on " + day.getDayOfWeek()
+ " will be " + day.getInfo("Conditions"));
}
我认为这就是你想要的。
答案 2 :(得分:1)
{
ArrayList<JTextArea> list = new ArrayList<JTextArea>() ;
list.add(day1);
//add day2 and day3 etc
Int i=0;
for (ForecastForday1 day : forecast)
{
//add check to see of list size is greater than i
list.get(i).append( //day data);
i=i+1;
}
}
答案 3 :(得分:1)
这是解决方案。谢谢@redDevil的帮助。它给了我这个想法。
int i=0;
for (ForecastForday1 day : forecast)
{
if (i==0){
day1Weather.append("The weather on " + day.getDayOfWeek() + " will be " + day.getInfo("Conditions")");
}
else if (i==1){
day2Weather.append("The weather on " + day.getDayOfWeek() + " will be " + day.getInfo("Conditions")");
}
else if (i==2){
day3Weather.append("The weather on " + day.getDayOfWeek() + " will be " + day.getInfo("Conditions")");
}
i++;
}