C++ 为什么广场的第二栏没有印刷在例外的地方?
很抱歉,如果问题不够清楚,那么我将在这里进一步解释:C++ 为什么广场的第二栏没有印刷在例外的地方?,c++,C++,很抱歉,如果问题不够清楚,那么我将在这里进一步解释: #include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; for (int y = 1; y <= num; ++y){ for (int x = 1; x <= num; +
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
for (int y = 1; y <= num; ++y){
for (int x = 1; x <= num; ++x){
if (x == 1 || x == num || y == 1 || y == num){
cout << "* ";
}
}
cout << endl;
}
}
我的预期输出应该是这样的:
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
有什么帮助吗?尤其是我的代码没有任何问题 不打印星号时,不打印任何空格。只需为该案例添加打印:
if (x == 1 || x == num || y == 1 || y == num) {
cout << "* ";
}
else {
cout << " "; // to align the * to the right
}
if(x==1 | | x==num | | y==1 | | y==num){
cout如果不打印星号,则不打印任何空格。只需为该情况添加打印:
if (x == 1 || x == num || y == 1 || y == num) {
cout << "* ";
}
else {
cout << " "; // to align the * to the right
}
if(x==1 | | x==num | | y==1 | | y==num){
除了“*”之外,你不能打印任何东西吗?那么,你希望矩形右边缩进的空格从哪里来呢{没有任何帮助吗?尤其是我的代码没有任何问题!-仅供参考,几乎没有人,甚至是专业程序员,编写程序,在第一个版本中,没有bug。你看到输出不是你想要的,所以要做的是调试你的代码,而不是简单地断定你的代码是“好的”不要试图自己调试这个问题。突击测验:例如,y
为2,x
为2时,你希望显示的代码打印什么?你只打印“*”以外的任何内容,那么你希望矩形右侧缩进的空格从何而来{没有任何帮助吗?尤其是我的代码没有任何问题!-仅供参考,几乎没有人,甚至是专业程序员,编写程序,在第一个版本中,没有bug。你看到输出不是你想要的,所以要做的是调试你的代码,而不是简单地断定你的代码是“好的”不要试图自己调试这个问题。突击测验:例如,y
为2,x
为2时,您希望显示的代码打印什么?