#include<iostream> using namespace std; int main() { string drinks[] = {"豆浆", "牛奶","果汁","酸梅汤"}; string foods[] = {"蛋糕", "油条", "饼干", "面包"}; int jLen = sizeof(drinks) / sizeof(drinks[0]); int iLen = sizeof(foods) / sizeof(foods[0]); for (int j = 0; j < jLen; j++) for (int i = 0; i < iLen; i++) cout << drinks[j] << " + " << foods[i] << endl; return 0; } 修改代码,使得数组值可以动态输入
时间: 2024-04-23 22:25:29 浏览: 105
#include<iostream>
using namespace std;
int main() {
int jLen, iLen;
cout << "请输入饮料数量:";
cin >> jLen;
string drinks[jLen]; // 动态定义饮料数组
cout << "请依次输入饮料名称:";
for (int i = 0; i < jLen; i++) {
cin >> drinks[i]; // 动态输入饮料名称
}
cout << "请输入食品数量:";
cin >> iLen;
string foods[iLen]; // 动态定义食品数组
cout << "请依次输入食品名称:";
for (int i = 0; i < iLen; i++) {
cin >> foods[i]; // 动态输入食品名称
}
for (int j = 0; j < jLen; j++) {
for (int i = 0; i < iLen; i++) {
cout << drinks[j] << " + " << foods[i] << endl;
}
}
return 0;
}
相关问题
#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; } 修改此代码,循环长度用数组长度判断
#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;
}
阅读全文