嵌套循环显示 25 个字符的行 c++
Nested loops to display rows of 25 characters c++
您好,我在使用嵌套循环输出用户选择的 25 个字符的行时遇到了一些问题。每行应该有 25 个字符,用户输入要打印的行数。
另外,每一行都需要比上一行多一个制表符。
目前我只能让程序输出一行字符,否则它会显示选中的一个字符的25行。
请帮忙。欢迎任何意见。
int lines=0, count=0, amount=0, symbol, i=0, j=0;
do {
cout << "\nEnter number of lines to print: ";
cin >> lines;
if (lines < 5 ) {
cout << "Please enter an integer greater than 5." << endl;
system("pause");
}
else if (lines >= 5) {
cout << "\nEnter the number corresponding to the character you would like to display: ";
cout << "\n 1. * \n 2. $ \n 3. # \n 4. ! \n 5. & \n ";
cin >> symbol;
for (i = 1; i <= 25; i++) {
cout << symbol << " " ;
count++;
for (j = 1; j <= lines; j++) {
cout << "" << endl;
}
}
}
} while (1);
你的嵌套循环方式不对。
尝试:
// Iterate parent loop line by line
for(j=1; j<=lines; j++){
// Add tabs according to the line number
for(int k=1; k<j; k++){
cout << "\t";
}
// Print symbol 25 times each line
for(i=1; i<=25; i++){
cout << symbol << " ";
}
cout << endl;
}
您好,我在使用嵌套循环输出用户选择的 25 个字符的行时遇到了一些问题。每行应该有 25 个字符,用户输入要打印的行数。
另外,每一行都需要比上一行多一个制表符。
目前我只能让程序输出一行字符,否则它会显示选中的一个字符的25行。
请帮忙。欢迎任何意见。
int lines=0, count=0, amount=0, symbol, i=0, j=0;
do {
cout << "\nEnter number of lines to print: ";
cin >> lines;
if (lines < 5 ) {
cout << "Please enter an integer greater than 5." << endl;
system("pause");
}
else if (lines >= 5) {
cout << "\nEnter the number corresponding to the character you would like to display: ";
cout << "\n 1. * \n 2. $ \n 3. # \n 4. ! \n 5. & \n ";
cin >> symbol;
for (i = 1; i <= 25; i++) {
cout << symbol << " " ;
count++;
for (j = 1; j <= lines; j++) {
cout << "" << endl;
}
}
}
} while (1);
你的嵌套循环方式不对。
尝试:
// Iterate parent loop line by line
for(j=1; j<=lines; j++){
// Add tabs according to the line number
for(int k=1; k<j; k++){
cout << "\t";
}
// Print symbol 25 times each line
for(i=1; i<=25; i++){
cout << symbol << " ";
}
cout << endl;
}