C++ C+是全新的+;还有一点被困在如何计算系列产品上。(说明如下)

C++ C+是全新的+;还有一点被困在如何计算系列产品上。(说明如下),c++,C++,您的程序应该提示用户输入两个整数,例如m和n,然后计算 并显示相应的系列产品(m×(m+1)×(m+2)× (n)− 1) ×n)。如果您感到困惑,例如用户输入3和9。然后 计算3x4x5x6x7x8x9的乘积。 它必须能够处理负数,到目前为止这不是问题,但无论我如何输入代码,它都不会返回产品 下面是我到目前为止所做的,我确信这对于有经验的程序员来说是非常糟糕的,但我假设我的错误在我的“for”语句中,任何帮助都将不胜感激 #include <iostream> using nam

您的程序应该提示用户输入两个整数,例如m和n,然后计算 并显示相应的系列产品(m×(m+1)×(m+2)× (n)− 1) ×n)。如果您感到困惑,例如用户输入3和9。然后 计算3x4x5x6x7x8x9的乘积。 它必须能够处理负数,到目前为止这不是问题,但无论我如何输入代码,它都不会返回产品

下面是我到目前为止所做的,我确信这对于有经验的程序员来说是非常糟糕的,但我假设我的错误在我的“for”语句中,任何帮助都将不胜感激

#include <iostream>

using namespace std;

int main()
{
     int a1, a2, a3, b1, b2, i;

    cout << "Input two integers separated by a blank space and press enter 
to display the corresponding series product.\n";
    cin >> a1 >> a2;

    if (a1 <= a2)
    {
        b1 = a1;
        b2 = a2;
    }
    else if (a2 <= a1)
    {
        b1 = a2;
        b2 = a1;
    }

    for (i =b1; i <= b2; i++)
    {
         a3 =* i;
    }

    cout << "The series product of "; cout << a1 << ", " << a2 << ", ";
    cout << " is: " << a3 << "\n" << endl;

     system("pause");
     return 0;
 }
#包括
使用名称空间std;
int main()
{
int a1、a2、a3、b1、b2、i;
cout>a1>>a2;

如果(a1您有一些小的格式错误。首先,请不要像在第一条语句中那样在键入字符串时换行。还要确保
cout
语句每行一条,以便于阅读

代码的其余部分没有问题,但是您确实需要在开始时设置
a3=1
,以便使其正确相乘。最后,您不能执行
a3=*i;
它必须是
a3*=i;

修正如下:

#include <iostream>

using namespace std;

int main()
{
    // Set A3 = 1 here.
     int a1, a2, a3 = 1, b1, b2, i;

    cout << "Input two integers separated by a blank space and press enter to display the corresponding series product.\n";
    cin >> a1 >> a2;

    if (a1 <= a2)
    {
        b1 = a1;
        b2 = a2;
    }
    else // Removed the else if here since an else works
    {
        b1 = a2;
        b2 = a1;
    }

    for (i = b1; i <= b2; i++)
    {
      // Change =* to *=
      a3 *= i;
    }

    cout << "The series product of ";
    cout << a1 << ", " << a2 << ", "; 
    cout << " is: " << a3 << endl;

     return 0;
 }
#包括
使用名称空间std;
int main()
{
//在此处设置A3=1。
int a1,a2,a3=1,b1,b2,i;
cout>a1>>a2;

if(a1)也必须允许用户以任何顺序输入整数(即2,5或5,2)请输入一个涵盖所有情况的示例。例如,如果用户输入2、5,预期输出是什么,您看到了什么?您从未初始化a3。您应该在
for
循环之前的某个位置添加
a3=1
。@SID捕捉得很好,没有考虑到这一点。修复:)请使用字符串文字连接,而不是打破列限制。这在任何阅读器上都更容易;-)