我在java中的代码接受两个int参数作为输入,生成一个数字数组。这是我的代码
public class Generator
{
public static void main(String[] args)
{
generate(500,7);
}
public static void generate(int goal, int days)
{
int[] list = new int[days];
int res;
res = goal/days;
int sum = 0;
int saved = res;
for(int i = 0; i < list.length; ++i)
{
list[i] += res;
//sum += list[i];
res += saved;
if(i == list.length - 1 && list[list.length - 1] < goal)
{
int difference = goal - list[list.length-1];
list[list.length-1] += difference;
}
System.out.println("day " + i + ": " + list[i]);
//System.out.println(sum);
}
}
}
结果是
day 0: 71
day 1: 142
day 2: 213
day 3: 284
day 4: 355
day 5: 426
day 6: 500
我想点击生成按钮时向用户显示此数据。
答案 0 :(得分:1)
简单的Listview就足够了。简单的实现只需要计算listview所需的行数,无论如何都是天数。 另外,您可以通过代码而不是xml来创建TextViews,并将创建循环数天。然后使用所需内容setText the textvies。
TextView实施
在xml中创建线性布局(myLinearLayout)并将其方向设置为vertical,然后
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is row #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}