c++ - 打印空心直角三角形

标签 c++

我在使用此 C++ 代码时遇到问题。它应该打印一个空心直角等腰三角形,但只是一遍又一遍地打印星号,所以 for 循环似乎被卡住了。

#include "pch.h"
#include <string>
#include <iostream>

int main() {
    int row;
    std::string s = "  ";
    std::string a = " *";
    int rows = 10;

    for (int i = 0; i < rows; i++) {

        if (i = 0) {
            std::cout << a << std::endl;
        }

        while (i > 2 && i < rows) {
            std::cout << a;

            for (int pos = 0; pos < i; pos++) {
                std::cout << s;
            }

            std::cout << a << std::endl;
        }

        std::cout << a << a << a << a << a << a << a << a << a << std::endl;

    }
}

最佳答案

您的 while 循环条件永远不会变为假,并且您需要在这一行中使用比较 (==) 而不是赋值:

        if (i = 0) {

关于c++ - 打印空心直角三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54039184/

相关文章:

c++ - 在 C++ 中使用排序谓词

c++ - 什么时候需要在 Ruby C 扩展中声明 volatile VALUE?

c++ - 使用没有功能的 extern "C"

c++ - 使用 ndk-build 链接现有的静态库

android - 在 Android 中读取共享首选项期间,Qt 上的 JNI 出现 NoSuchMethodError

c++ - 在带有 GPU 的 Halide 上使用 extern

c++ - 为不同的形状实现抽象的 'overlaps' 方法?

c++ - 编译一个简单的 Gsoap 服务

c++ - 在 Boost.Spirit 中解析开头之前的短语结尾

c++ - 将类接口(interface)转换为类模板