#include<iostream> using namespace std; int main() { int arrTest[] = {1,3,4}; //int dlen = length(arrTest); cout <<length(arrTest)<<endl; string drinks[] = {"豆浆", "牛奶","果汁"}; string foods[] = {"蛋糕", "油条", "饼干", "面包"}; for (int j = 0; j < 3; j++) for (int i = 0; i < 4; i++) cout << drinks[j] << " + " << foods[i] << endl; return 0; } 修改此代码,循环长度用数组长度判断
时间: 2024-04-23 08:25:41 浏览: 141
使用include <iostream>时一定要加using namespace std
#include<iostream>
using namespace std;
int main() {
int arrTest[] = {1,3,4};
int dlen = sizeof(arrTest) / sizeof(arrTest[0]); // 计算数组长度
cout << dlen << endl;
string drinks[] = {"豆浆", "牛奶","果汁"};
string foods[] = {"蛋糕", "油条", "饼干", "面包"};
for (int j = 0; j < sizeof(drinks) / sizeof(drinks[0]); j++) { // 使用数组长度进行循环
for (int i = 0; i < sizeof(foods) / sizeof(foods[0]); i++) {
cout << drinks[j] << " + " << foods[i] << endl;
}
}
return 0;
}
阅读全文