gpt4 book ai didi

c++ - 简单的for循环我想不通

转载 作者:行者123 更新时间:2023-11-28 00:11:30 25 4
gpt4 key购买 nike

我是 C++ 的新手,所以昨晚我想到了一些事情。我想打印出 1-100 的数字,但每行 10 个数字。我知道下面的代码是错误的,因为它只是垂直打印 1-100。如果有人能阐明我的问题,将不胜感激。感谢阅读:)

#include <iostream>
using namespace std;

int main() {

for(int x = 1; x <= 100; x++) {
cout << x << endl;

}
}

最佳答案

所以你想打印 10 个数字,然后是一个回车符,然后是 10 个数字,然后是一个回车符,依此类推,对吗?

如果是这样,怎么样:

for(int x = 1; x <= 100; x++) {
cout << x << " ";
if ((x%10)==0) cout << endl;
}

关于c++ - 简单的for循环我想不通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32877619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com