我正在编写此方法,该方法接受3个参数,然后在有序行中输出结果。我很好地理解了这一点,但是由于某种原因,在每次迭代结束时我得到一个重复的数字。如:



这是完整的代码。希望能帮助到你:

#include <iostream>
//function prototype:
int payoff(int x, int y, int z);

//global variables:
int R1;
int R2;
int R3;
int total;

using namespace std;
int main(void) {
    cout << "R1\t R2\t R3\t\n" << endl;

    ////////////////////
    //first loop
    ////////////////////
    R1 = 1;
    R2 = 1;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //second loop
    ////////////////////
    R1 = 1;
    R2 = 2;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //third loop
    ////////////////////
    R1 = 1;
    R2 = 3;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //fourth loop
    ////////////////////
    R1 = 2;
    R2 = 1;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //fifth loop
    ////////////////////
    R1 = 2;
    R2 = 2;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //sixth loop
    ////////////////////
    R1 = 2;
    R2 = 3;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //seventh loop
    ////////////////////
    R1 = 3;
    R2 = 1;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //eight loop
    ////////////////////
    R1 = 3;
    R2 = 2;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    ////////////////////
    //ninth loop
    ////////////////////
    R1 = 3;
    R2 = 3;
    R3 = 1;
    printf("%d", payoff(R1, R2, R3));
    printf("\n");

    system("PAUSE");
    return 0;
}
/////////////////////////////////////////////////////////
//FUNCTIONS:
/////////////////////////////////////////////////////////
int payoff(int R1, int R2, int R3) {
    do {
        //calculate payoff:
        total = R1;
        if (R2 < R1) {
            total = total + R2;
        } //end if
        else {
            if (R3 < R1) {
                total = total + R3;
            } //end if
            else {
                total = R1;
            } // end else
        } //end else

        if (R3 < R2) {
            total = total + (2 * R3);
        } //end if
        else {
            if (R3 < R1) {
                total = total + R3;
            } //end if
            else {
                total = R1;
            } //end else
        } //end else

        //display payoff:
        printf("%d\t %d\t %d\t payoff is %d\n", R1, R2, R3, total);

        R3++;
    } while (R3 < 4); //end do-while

    return total;
} //end function payoff


谁能帮我弄清楚如何消除这个讨厌的多余数字?非常感谢您!

最佳答案

payoff()的返回代码将打印在不需要的printf语句中。您只需要在不使用payoff()的情况下调用printf

printf("%d", payoff(R1, R2, R3));调用payoff函数,然后输出从payoff()函数返回的值。

因此,请替换以下行:

 printf("%d", payoff(R1, R2, R3));




 payoff(R1, R2, R3);

关于c++ - 使用功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22283267/

10-13 00:01