gpt4 book ai didi

c++ - For循环不太有效

转载 作者:行者123 更新时间:2023-11-30 04:20:22 26 4
gpt4 key购买 nike

这是我第一次尝试 for 循环,我遇到了一些问题。我正在尝试编写一个程序,询问两支球队每节得分多少,然后显示总分和获胜球队。

#include <iostream>

using namespace std;

int main( )
{
int scoreA = 0;
int scoreB = 0;

cout << "This program calculates the average score of 10 tests." << endl;

for (int counter = 0; counter < 4; counter = counter + 1)
{
cout << "Enter Team A's quarterly points: ";
cin >> scoreA;
cout << "Enter Team B's quarterly points: ";
cin >> scoreB;
scoreA = scoreA + scoreA;
scoreB = scoreB + scoreB;
}

cout << "Team A's Score: " << scoreA << endl;
cout << "Team B's Score: " << scoreB << endl;
if (scoreA > scoreB)
{
cout << "Team A wins";
}
else
{
cout << "Team B wins";
}

system("pause");
return 0;
}

最佳答案

你并没有具体说明什么不适用于循环,但我在你的 for 循环中看到以下内容:

cout << "Enter Team A's quarterly points: ";
cin >> scoreA;
cout << "Enter Team B's quarterly points: ";
cin >> scoreB;
scoreA = scoreA + scoreA;
scoreB = scoreB + scoreB;

因此,您在每次迭代时都会覆盖 scoreA 和 scoreB 中的分数 (cin >> scoreA),然后将它们加倍 (scoreA = scoreA + scoreA)。

关于c++ - For循环不太有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15326289/

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