传递参数 [英] Passing Parameters

查看:63
本文介绍了传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的代码。这是一个简单的游戏程序

掷骰子 - 非常基本。

我已经将gameStatus声明为全局变量。我想知道是否有

是一种更好的处理方式(可能通过局部变量)

我正在使用该变量来跟踪游戏的状态并通过

从craps()返回main(),我要么增加或减少银行

余额。


这应该是用本地参数处理?这会是一个更好的方法。

如果是这样,我该怎么做?


谢谢


关注是代码:


#include< iostream>

#include< cstdlib>

#include< ctime>


使用命名空间std;


int rollDice();

int craps();


enum状态{CONTINUE,WON,LOST};

状态gameStatus;


int main(){


int bankBalance = 1000;

int wager;

char answer;


do {

cout<<"下注:&;;

cin>> wager;


while(wager> ; = bankBalance){

cout<<" \ n你的银行余额是:<<<< bankBalance<< endl;

cout<< 请输入有效的投注:<<< endl;

cin>>投注;

}

craps() ;


if(gameStatus == WON){

bankBala nce = bankBalance + wager;

cout<<"您的银行余额为$"<< bankBalance<< endl;


}

else {

bankBalance = bankBalance - wager;

cout<<"您的银行余额为$"<< bankBalance< ;< endl;

}


cout<<"你想再玩一次吗? (y / n)" ;;

cin>>回答;


} while(answer ==''y''|| answer == '' Y ''); //结束时间


返回0;

}


int craps(){


int sum,myPoint;


srand(time(0));

sum = rollDice();


开关(总和){


案例7:

案例11:

gameStatus = WON;

休息;


案例2:

案例3:

案例12:

gameStatus = LOST;

休息;

默认值:

gameStatus = CONTINUE;

myPoint = sum;

cout<<" Point is:"<< myPoint<< endl;


}


while(gameStatus == CONTINUE){

sum = rollDice();


if(sum == myPoint)

gameStatus = WON;

else if(sum == 7)

gameStatus = LOST;

}


if(gameStatus == WON)

cout<<" Player wins"<< endl;

else

cout<<" Player Loses"<< endl;

返回(gameStatus);

}


int rollDice(){


int die1 ,die2,workSum;


die1 = 1 + rand()%6;

die2 = 1 + rand()%6;


workSum = die1 + die2;

cout<<" Player rolled:"<<< die1<<"" +"<<< die2<<" ="<< workSum<< endl;


返回workSum;


}


解决方案

"<<< bankBalance<< endl;


}

else {

bankBalance = bankBalance - wager;

cout<<"您的银行余额是


" << bankBalance<< endl;

}


cout<<"你想再玩一次吗? (y / n)" ;;

cin>>回答;


} while(answer ==''y''|| answer == '' Y ''); //结束时间


返回0;

}


int craps(){


int sum,myPoint;


srand(time(0));

sum = rollDice();


开关(总和){


案例7:

案例11:

gameStatus = WON;

休息;


案例2:

案例3:

案例12:

gameStatus = LOST;

休息;

默认值:

gameStatus = CONTINUE;

myPoint = sum;

cout<<" Point is:"<< myPoint<< endl;


}


while(gameStatus == CONTINUE){

sum = rollDice();


if(sum == myPoint)

gameStatus = WON;

else if(sum == 7)

gameStatus = LOST;

}


if(gameStatus == WON)

cout<<" Player wins"<< endl;

else

cout<<" Player Loses"<< endl;

返回(gameStatus);

}


int rollDice(){


int die1 ,die2,workSum;


die1 = 1 + rand()%6;

die2 = 1 + rand()%6;


workSum = die1 + die2;

cout<<" Player rolled:"<<< die1<<"" +"<<< die2<<" ="<< workSum<< endl;


返回workSum;


}



Sonia< sm ***** @ hotmail.com>在留言中写道

新闻:og ************ @ bignews5.bellsouth.net ...

请看下面的内容码。这是一个简单的掷骰子游戏程序 - 非常基本。
我已经将gameStatus声明为全局变量。我想知道
是否有更好的处理方式(可能通过局部变量)
我使用该变量来跟踪游戏的状态并将其传递回main ()来自掷骰子(),我要么增加或减少银行的平衡。

这应该用本地参数来处理吗?这会是一种更好的方式。
如果是这样,我该怎么做?


你不需要做任何这些。见下文。

谢谢

以下是代码:

#include< iostream>
#include< cstdlib>
#include< ctime>

使用命名空间std;

int rollDice();
int craps();
enum状态{CONTINUE,WON,LOST};


状态gameStatus;


删除此行。

int main(){
int bankBalance = 1000;
int wager; < char /> char answer;

做{
cout<<"下注:" ;;
cin>> wager;

while(wager> = bankBalance){
cout<<" \ n你的银行余额是:<<< bankBalance<< endl;
cout<<"输入有效的投注:"<< endl;
cin>>投注;
}

craps();


删除此行。


if(gameStatus == WON){


替换为:


if(craps()== WON){

bankBalance = bankBalance + wager;
cout<<"你的银行存款是

Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.

Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?

Thanks

Following is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice();
int craps();

enum Status {CONTINUE, WON, LOST};
Status gameStatus;

int main(){

int bankBalance = 1000;
int wager;
char answer;

do {
cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}
craps();

if (gameStatus == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;

}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}

cout<<"Do you want to play again? (y/n)";
cin>>answer;

} while( answer ==''y'' || answer == ''Y''); // end of while

return 0;
}

int craps() {

int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;

return(gameStatus);
}

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;

}


解决方案

"<<bankBalance<<endl;

}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is


"<<bankBalance<<endl;
}

cout<<"Do you want to play again? (y/n)";
cin>>answer;

} while( answer ==''y'' || answer == ''Y''); // end of while

return 0;
}

int craps() {

int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;

return(gameStatus);
}

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;

}



Sonia <sm*****@hotmail.com> wrote in message
news:og************@bignews5.bellsouth.net...

Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.

Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?
You don''t need to do any of that. See below.

Thanks

Following is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice();
int craps();

enum Status {CONTINUE, WON, LOST};
Status gameStatus;
Remove this line.

int main(){

int bankBalance = 1000;
int wager;
char answer;

do {
cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}

craps();
Remove this line.
if (gameStatus == WON) {
Replace with:

if (craps() == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is


这篇关于传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆