摇滚计划 [英] Rock Program

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

问题描述




我正在做一个摇滚,剪刀,纸上活动。我认为我有大部分

它,但我的主要功能有问题,这是我的代码:

#include< stdio.h>

#include< stdlib.h> / *为rand()和srand()* /

#include< time.h> / *表示时间()* /

/ *用户输入R,P,S,Q * /

int getValidInteger(int min,int max,char prompt [] )

{

char userInput;

printf(" \ nEnter R(ock),P(aper)或S(cissors) )或Q(uit):);

scanf("%c"& userInput);

// userInput = toupper(userInput);

开关(userInput){

案例''Q'':返回0;

案例''R'':返回1;

case''P'':返回2;

case''S'':返回3;

默认值:printf("无效选择!%s \ n");

}

}

/ *计算机Cal它的选择* /

int getCompChoice()

{

int comp = rand()%3;

switch(comp){

案例0:返回''R'';

案例1:返回''P'';

案例2:返回''S'' ;

}

返回comp;

}


void result(int user,int comp )

{


if(user = comp)

printf(" Draw \ n");

switch(user){

案例1:

if(comp ==''R'')

printf(" You Win\ n);

案例2:

if(comp ==''P'')

printf(" You Win\ n);

案例3:

if(comp ==''S'')

printf(" You Win\ n);

默认值:

printf(&You Loose\\\
);

退出(1);

}


返回;

}

main(无效)

{

int score = 0;

srand(time(NULL)); / * seed随机数生成器* /

int getValidInteger();

if(getValidInteger = getCompChoice)得分++;

得分;

}

我想我会以错误的方式去做。有人可以指导我的方向,而不是给我答案。

GC

Hi

I''m working on a rock, scissors, paper program. I think I have most of
it, but I am having trouble with my main function, here is my code:
#include <stdio.h>
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
{
char userInput;
printf("\nEnter R(ock), P(aper),or S(cissors) or Q(uit): ");
scanf("%c",&userInput);
//userInput=toupper(userInput);
switch(userInput){
case ''Q'':return 0;
case ''R'':return 1;
case ''P'':return 2;
case ''S'':return 3;
default: printf("Invalid Choice!%s\n");
}
}
/*Computer Cal it''s choice*/
int getCompChoice()
{
int comp = rand()%3;
switch (comp){
case 0: return ''R'';
case 1: return ''P'';
case 2: return ''S'';
}
return comp;
}

void result(int user, int comp)
{

if (user=comp)
printf("Draw\n");
switch (user) {
case 1:
if (comp == ''R'')
printf("You Win\n");
case 2:
if (comp == ''P'')
printf("You Win\n");
case 3:
if (comp == ''S'')
printf("You Win\n");
default:
printf("You Loose\n");
exit(1);
}

return;
}
main(void)
{
int score = 0;
srand(time(NULL));/* seed the random number generator*/
int getValidInteger();
if (getValidInteger=getCompChoice) score++;
return score;
}
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.
GC

推荐答案

gc写道:


我正在研究摇滚,剪刀,纸质程序。我认为我有最多的,但我的主要功能有问题,这是我的代码:
#include< stdio.h>
#include< stdlib.h> ; / *为rand()和srand()* /
#include< time.h> / *表示时间()* /

/ *用户输入R,P,S,Q * /
int getValidInteger(int min,int max,char prompt [])
{
char userInput;
printf(" \ nEnter R(ock),P(aper)或S(cissors)或Q(uit):");
scanf ("%c"& userInput);
// userInput = toupper(userInput);
switch(userInput){
case''Q'':return 0;
case''R'':返回1;
case''P'':返回2;
case''S'':返回3;
默认值:printf("无效的选择!%s \ n");
}
}
/ *计算机Cal它的选择* /
int getCompChoice()
{
int comp = rand()%3;
switch(comp){
案例0:返回''R'';
案例1:返回''P''; <案例2:返回''S'';
}
返回comp;
}

void result(int user,int comp)
{

if(user = comp)
printf(" Draw \ n");
switch(用户){
案例1: if(comp ==''R'')
printf(You Win \ n);
案例2:
if(comp ==''P'')
printf(" You Win\ n ;);
案例3:
if(comp ==''S'')
printf(You Win\ n);
默认值:
printf(" You Loose\\\
);
退出(1);
}

返回;
}

main(void)
{得分= 0;
srand(time(NULL)); / *种子随机数生成器* /
int getValidInteger();
if(getValidInteger = getCompChoice)得分++;
返回得分;
}

我想我会以错误的方式解决它。有人可以引导我朝着正确的方向前进,而不会给我答案。


有一些问题,但是当你提到''main''时,也许这是一个很好的

起点。


这不是必需的 - 因为编译器已经看到了这个函数的定义

。 int getValidInteger();


getValidInteger和getCompChoice - 当像这样使用时解析这些函数的

地址 - 即常数值。然后你试图将
分配给另一个常数 - 你的意思是==而不是=?要调用

函数......好吧,你知道怎么做。 if(getValidInteger = getCompChoice)
Hi

I''m working on a rock, scissors, paper program. I think I have most
of it, but I am having trouble with my main function, here is my code:
#include <stdio.h>
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
{
char userInput;
printf("\nEnter R(ock), P(aper),or S(cissors) or Q(uit): ");
scanf("%c",&userInput);
//userInput=toupper(userInput);
switch(userInput){
case ''Q'':return 0;
case ''R'':return 1;
case ''P'':return 2;
case ''S'':return 3;
default: printf("Invalid Choice!%s\n");
}
}
/*Computer Cal it''s choice*/
int getCompChoice()
{
int comp = rand()%3;
switch (comp){
case 0: return ''R'';
case 1: return ''P'';
case 2: return ''S'';
}
return comp;
}

void result(int user, int comp)
{

if (user=comp)
printf("Draw\n");
switch (user) {
case 1:
if (comp == ''R'')
printf("You Win\n");
case 2:
if (comp == ''P'')
printf("You Win\n");
case 3:
if (comp == ''S'')
printf("You Win\n");
default:
printf("You Loose\n");
exit(1);
}

return;
}
main(void)
{
int score = 0;
srand(time(NULL));/* seed the random number generator*/
int getValidInteger();
if (getValidInteger=getCompChoice) score++;
return score;
}
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.
There are a few problems, but as you mention ''main'', perhaps that''s a good
place to start.

This is not required - as the compiler has already seen the definition of
this function. int getValidInteger();
getValidInteger and getCompChoice - when used like this resolve to the
addresses of those functions - i.e., constant values. You''re then trying to
assign one constant to another - did you mean == instead of = ? To invoke a
function ... well, you know how to do that. if (getValidInteger=getCompChoice)




你可能想在代码中检查== vs. =的其他问题,

也你打电话给printf /老实说/整个。

-

==============

不是学生

==============



You might want to check for other problems with == vs. = in the code, and
also that you''re calling printf /honestly/ throughout.
--
==============
Not a pedant
==============


2006年5月4日23:05: 02 -0700,gc < GR ********* @ bigpond.com>写道:
On 4 May 2006 23:05:02 -0700, "gc" <gr*********@bigpond.com> wrote:


我正在研究摇滚,剪刀,纸质程序。我认为我有大部分内容,但我的主要功能有问题,这是我的代码:
#include< stdio.h>
#include< stdlib.h> ; / *为rand()和srand()* /
#include< time.h> / *表示时间()* /

/ *用户输入R,P,S,Q * /
int getValidInteger(int min,int max,char prompt [])


这些参数是什么?

{
char userInput;
printf(" \ nEnter R(ock),P(aper) ),或S(cissors)或Q(uit):");
scanf("%c",& userInput);
// userInput = toupper(userInput);
switch(userInput){
case''Q'':返回0;
case''R'':返回1;
case''P'':返回2;
case''S'':返回3;
默认值:printf(无效选择!%s \ n);
}


如果您要多次调用此函数,则需要

删除位于缓冲区中的''\ n''(来自ENTER键)。

否则,您的下一个电话将导致错误消息。


如果用户确实输入了无效字符,那么让我们很高兴

他重新进入。

}
/ *电脑Cal它是'choi' ce * /
int getCompChoice()


int getCompChoice(void)

{
int comp = rand()%3;
switch(comp){
案例0:返回''R'';
案例1:返回''P'';
案例2:返回''S'' ;
}
返回comp;
}
void结果(int user,int comp)
{

if( user = comp)
printf(" Draw \ n");
switch(用户){
案例1:
if(comp ==''R'')
printf(You Win \ n);


你需要在每个案件之间有一个休息声明。

案例2:
if(comp ==''P'')
printf(You Win \ n);
案例3:
if(comp ==''S'')
printf(" You Win\ n) ;
默认:
printf(" You Loose\\\
);
exit(1);


一次丢失后你退出整个计划?使用EXIT_FAILURE代替

为1的便携性。

}

返回;
}

main( void)


int main(无效)

{
int score = 0;
srand(time(NULL)); / *播种随机数生成器* /
int getValidInteger();


getValidInteger目前需要三个参数。

if(getValidInteger = getCompChoice)得分++;


肯定会给你一个语法错误。如果没有括号,

未加修饰的函数名称将计算为函数的地址。 =左边的
不是可修改的l值,因此无法收到getCompChoice的

地址。


我想想你想要的就是这样的循环


获取用户的输入

如果退出

退出循环
计算机的选择

确定游戏结果(并保持统计数据?)

重复循环

返回得分;


退出循环后,您可以打印统计数据,但是您应该返回

0(或EXIT_SUCCESS)以表示程序正常完成。

}

我想我会以错误的方式解决这个问题。有人可以引导我朝正确的方向前进,而不给我答案。
Hi

I''m working on a rock, scissors, paper program. I think I have most of
it, but I am having trouble with my main function, here is my code:
#include <stdio.h>
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
What are these parameters for?
{
char userInput;
printf("\nEnter R(ock), P(aper),or S(cissors) or Q(uit): ");
scanf("%c",&userInput);
//userInput=toupper(userInput);
switch(userInput){
case ''Q'':return 0;
case ''R'':return 1;
case ''P'':return 2;
case ''S'':return 3;
default: printf("Invalid Choice!%s\n");
}
If you are going to call this function more than once, you need to
remove the ''\n'' (from the ENTER key) that is sitting in the buffer.
Otherwise, your next call will result in the error message.

If the user does enter an invalid character, it would be nice to let
him re-enter.
}
/*Computer Cal it''s choice*/
int getCompChoice()
int getCompChoice(void)
{
int comp = rand()%3;
switch (comp){
case 0: return ''R'';
case 1: return ''P'';
case 2: return ''S'';
}
return comp;
}

void result(int user, int comp)
{

if (user=comp)
printf("Draw\n");
switch (user) {
case 1:
if (comp == ''R'')
printf("You Win\n");
You need a break statement between each case.
case 2:
if (comp == ''P'')
printf("You Win\n");
case 3:
if (comp == ''S'')
printf("You Win\n");
default:
printf("You Loose\n");
exit(1);
After one loss you quit the whole program? Use EXIT_FAILURE instead
of 1 for portability.
}

return;
}
main(void)
int main(void)
{
int score = 0;
srand(time(NULL));/* seed the random number generator*/
int getValidInteger();
getValidInteger currently requires three arguments.
if (getValidInteger=getCompChoice) score++;
Surely this gave you a syntax error. Without the parentheses, an
unadorned function name evaluates to the address of the function. The
left of the = is not a modifiable l-value so it cannot receive the
address of getCompChoice.

I think what you want here is a loop like

get user''s input
if quit
exit loop
compute computer''s choice
determine result of game (and keep statistics?)
repeat loop
return score;
After exiting the loop, you may print the stats but you should return
0 (or EXIT_SUCCESS) to indicate normal completion of the program.
}
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.




如果result()返回指示谁赢了,可能会更容易

消息和统计信息在main()中处理。

删除电子邮件地址



It might be easier if result() returned an indicator of who won and
the messages and statistics were processed in main().
Remove del for email


gc< gr *********@bigpond.com>写道:
gc <gr*********@bigpond.com> wrote:
/ *用户输入R,P,S,Q * /
int getValidInteger(int min,int max,char prompt [])
{
char userInput;
printf(" \ nEnter R(ock),P(aper)或S(cissors)或Q(uit):");
scanf(" ;%c",& userInput);
// userInput = toupper(userInput);
switch(userInput){
case''Q'':返回0;
case''R'':返回1;
case''P'':返回2;
case''S'':返回3;
默认值:printf("无效选择!%s \ n");
}
}


正如Barry所说,你的用户(包括你的导师,如果这是一个

课程作业)如果有必要,可能还想再次输入一个

字符。使用scanf()充满了危险和

陷阱,所以我衷心建议使用更多

直接getc(),因为它足以完成你的任务。 />
main(无效)


您可能不感兴趣,但没有

指定的返回类型的函数是no根据C99

标准允许的时间更长。即使它是合法的也不是一个好主意,所以你

应该简单地写一下


int main(void)

>
并完全避免这种情况。

我想我会以错误的方式解决这个问题。有人可以引导我走向正确的方向,而不给我答案。
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
{
char userInput;
printf("\nEnter R(ock), P(aper),or S(cissors) or Q(uit): ");
scanf("%c",&userInput);
//userInput=toupper(userInput);
switch(userInput){
case ''Q'':return 0;
case ''R'':return 1;
case ''P'':return 2;
case ''S'':return 3;
default: printf("Invalid Choice!%s\n");
}
}
As Barry noted, your users (including your instructor, if this is a
class assignment) would probably like another chance to enter a
character if necessary. Using scanf() is fraught with perils and
gotchas, so I would heartily recommend using the much more
straightforward getc() since it is adequate for your task.
main(void)
It''s probably not of interest to you, but functions without a
specified return type are no longer permissible under the C99
standard. It wasn''t a great idea even when it was legal, so you
should simply write

int main( void )

and avoid the situation altogether.
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.




你看起来很近。观察到错误的皮特

指出 - 使用''=''而不是''=='' - 对你的
计划的行为产生重大影响并修复它们(至少有两个)将起作用

奇迹。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



You look like you''re pretty close. Observe that the mistake pete
pointed out - using ''='' instead of ''=='' - has a major impact on your
program''s behavior, and fixing them (there are at least two) will work
wonders.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


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

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