第1个回答 2023-04-14
以下是C++语言的代码示例:
#include <iostream>
using namespace std;
void fun(int n, char ch) {
for (int i = 0; i < n; i++) {
cout << ch;
}
cout << endl;
}
int main() {
int n;
char ch;
cout << "请输入字符个数n和指定字符ch(用空格隔开):" << endl;
cin >> n >> ch;
fun(n, ch);
return 0;
}
其中,fun函数接受两个参数:n表示要生成的指定字符个数,ch表示指定的字符。函数使用一个循环来生成指定字符,并在最后输出一个换行符以完成一行的输出。
在主函数中,先提示用户输入参数,然后调用fun函数进行输出。