gpt4 book ai didi

c++ - 简单的 C++ 语法更正

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

下面的 C++ 代码有什么问题吗?

int i;
int n;
cout << "a";
cin >> n;
int player[i];
for(i = 0; i <= 3; i++)
{
player[i] = n;
}

最佳答案

要获得相同的功能,我会这样做:

#define PLAYER_SIZE 4
//Or you can go:
//const int PLAYER_SIZE = 4;
int n = 0; //Don't *need* to = 0 in this case, but good practice.
cout << "a";
cin >> n;
int player[ PLAYER_SIZE ];
for( int i = 0; i < PLAYER_SIZE; ++i )
{
player[i] = n;
}

因为在这种情况下 i<=3 是硬编码的,所以没有必要高于 4。

关于c++ - 简单的 C++ 语法更正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593850/

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