我正在编写这个方法,它接受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
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我弄清楚如何摆脱那个讨厌的额外数字?非常感谢你提前!
返回代码payoff()
将打印在printf
您不想要的语句中.你只需要打电话payoff()
就可以了printf
.
printf("%d", payoff(R1, R2, R3));
调用支付函数,然后输出payoff()函数返回的值.
所以替换以下行:
printf("%d", payoff(R1, R2, R3));
Run Code Online (Sandbox Code Playgroud)
同
payoff(R1, R2, R3);
Run Code Online (Sandbox Code Playgroud)