C++ 代码实现海面波浪效果
#include
using namespace std;
int main() {
// 设定海面的宽度和高度
const int WIDTH = 80;
const int HEIGHT = 20;
// 创建一个二维数组来表示海面
char sea[HEIGHT][WIDTH];
// 初始化海面,用空格填充
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
sea[i][j] = ' ';
}
}
// 在海面上绘制波浪
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if ((i+j) % 2 == 0) {
sea[i][j] = '~';
}
}
}
// 在海面上打印出来
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
cout << sea[i][j];
}
cout << endl;
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/nQJ7 著作权归作者所有。请勿转载和采集!