编程中的数组应用与实践
1. 数组图形绘制
1.1 创建图形绘制程序
首先,我们要创建一个程序来绘制存储在 temperatures
数组中的值的图形。操作步骤如下:
1. 选择 File > New
开始一个新程序,并将其保存为 GraphTemps
。
2. 定义温度数组:
int[] temperatures = {57, 10, 32, 78, 212, 101};
- 设置画布大小为 250 像素×250 像素:
size(250, 250);
- 创建变量存储图形相关参数:
int spaceBetween = 35; // 相邻条形图顶部之间的距离
int barHeight = 10; // 每个条形图的高度
1.2 绘制条形图
使用 for
循环遍历数组,绘制每个条形图:
int barTop;
for (int count = 0; count < temperatures.length; c