同样的输出不管输入。

时间:2022-07-04 03:13:31

So I'm writing this program to be a two player game of paper rock scissors, and no matter what each player chooses, the output is "Player 1 wins."

所以我写这个程序是为了玩两款游戏的石头剪刀,不管每个玩家选择什么,输出都是“玩家1赢”。

#include <stdio.h>
int main(void)
{
long player1Choice, player2Choice, p, r, s;


        printf("Player 1, enter paper (p), rock (r), or scissors (s)\n");
            player1Choice=getchar();
            getchar();

        printf("Player 2, enter paper (p), rock (r), or scissors (s)\n");
            player2Choice=getchar();
            getchar();

        if((player1Choice=p)&&(player2Choice=r))    
            printf("Player 1 wins!\n");
        else if((player1Choice=r)&&(player2Choice=p))
            printf("Player 2 wins!\n");
        else if((player1Choice=r)&&(player2Choice=s))
            printf("Player 1 wins!\n");
        else if((player1Choice=s)&&(player2Choice=r))
            printf("Player 2 wins!\n");
        else if((player1Choice=s)&&(player2Choice=p))
            printf("PLayer 1 wins!\n");
        else if((player1Choice=p)&&(player2Choice=s))
            printf("Player 2 wins!\n");


    printf("Press any key to exit");
    getchar();
    return 0;
}

I think the logical "and"s in my "if" statements may be causing the trouble, but I'm not sure.

我认为逻辑上的“和”在我的“如果”陈述可能引起麻烦,但我不确定。

2 个解决方案

#1


2  

You're missing single quotes around your character constants and also using = when you need ==. So change e.g.

您在字符常量周围缺少单引号,并且在需要时也使用==。所以改变如。

    if((player1Choice=p)&&(player2Choice=r))    

to:

:

    if((player1Choice=='p')&&(player2Choice=='r'))    

Do this for all similar occurrences.

对所有类似的事件都这样做。

Also get rid of the unused variable, r, p, and s.

也可以去掉未使用的变量r p和s。

And finally, turn on compiler warnings and take notice of them - the compiler would have helped you fix all these problems if you had allowed it to.

最后,打开编译器警告并注意它们——如果您允许的话,编译器将帮助您解决所有这些问题。

#2


1  

You have declared p, r, and s, but you never initialize them. You also are using assignment (=) rather than a test for equality (==). Your program causes undefined behaviour. It looks like you intended something along the lines of:

您已经声明了p、r和s,但您从未初始化它们。您还使用了赋值(=)而不是对等式(==)的测试。您的程序导致未定义的行为。看起来你想要的是:

if ((player1Choice == 'p') && (player2Choice == 'r'))

After fixing those, you can get rid of the spurious variables. Alternately, change your variable declarations to include initialization:

在修正了这些之后,你就可以摆脱这些伪变量了。另外,将变量声明更改为包括初始化:

long player1Choice, player2Choice, p = 'p', r = 'r', s = 's';

You'll still need to fix your = problem.

您仍然需要解决=问题。

You should turn more warnings on in your compiler. For example, for your program, from Clang:

您应该在编译器中打开更多的警告。例如,对于您的程序,从Clang:

$ clang -Wall example.c -o example
example.c:19:51: warning: variable 's' is uninitialized when used here
      [-Wuninitialized]
        else if((player1Choice=r)&&(player2Choice=s))
                                                  ^
example.c:4:43: note: initialize the variable 's' to silence this warning
long player1Choice, player2Choice, p, r, s;
                                          ^
                                           = 0
example.c:15:27: warning: variable 'p' is uninitialized when used here
      [-Wuninitialized]
        if((player1Choice=p)&&(player2Choice=r))    
                          ^
example.c:4:37: note: initialize the variable 'p' to silence this warning
long player1Choice, player2Choice, p, r, s;
                                    ^
                                     = 0
example.c:15:46: warning: variable 'r' is uninitialized when used here
      [-Wuninitialized]
        if((player1Choice=p)&&(player2Choice=r))    
                                             ^
example.c:4:40: note: initialize the variable 'r' to silence this warning
long player1Choice, player2Choice, p, r, s;
                                       ^
                                        = 0
3 warnings generated.

#1


2  

You're missing single quotes around your character constants and also using = when you need ==. So change e.g.

您在字符常量周围缺少单引号,并且在需要时也使用==。所以改变如。

    if((player1Choice=p)&&(player2Choice=r))    

to:

:

    if((player1Choice=='p')&&(player2Choice=='r'))    

Do this for all similar occurrences.

对所有类似的事件都这样做。

Also get rid of the unused variable, r, p, and s.

也可以去掉未使用的变量r p和s。

And finally, turn on compiler warnings and take notice of them - the compiler would have helped you fix all these problems if you had allowed it to.

最后,打开编译器警告并注意它们——如果您允许的话,编译器将帮助您解决所有这些问题。

#2


1  

You have declared p, r, and s, but you never initialize them. You also are using assignment (=) rather than a test for equality (==). Your program causes undefined behaviour. It looks like you intended something along the lines of:

您已经声明了p、r和s,但您从未初始化它们。您还使用了赋值(=)而不是对等式(==)的测试。您的程序导致未定义的行为。看起来你想要的是:

if ((player1Choice == 'p') && (player2Choice == 'r'))

After fixing those, you can get rid of the spurious variables. Alternately, change your variable declarations to include initialization:

在修正了这些之后,你就可以摆脱这些伪变量了。另外,将变量声明更改为包括初始化:

long player1Choice, player2Choice, p = 'p', r = 'r', s = 's';

You'll still need to fix your = problem.

您仍然需要解决=问题。

You should turn more warnings on in your compiler. For example, for your program, from Clang:

您应该在编译器中打开更多的警告。例如,对于您的程序,从Clang:

$ clang -Wall example.c -o example
example.c:19:51: warning: variable 's' is uninitialized when used here
      [-Wuninitialized]
        else if((player1Choice=r)&&(player2Choice=s))
                                                  ^
example.c:4:43: note: initialize the variable 's' to silence this warning
long player1Choice, player2Choice, p, r, s;
                                          ^
                                           = 0
example.c:15:27: warning: variable 'p' is uninitialized when used here
      [-Wuninitialized]
        if((player1Choice=p)&&(player2Choice=r))    
                          ^
example.c:4:37: note: initialize the variable 'p' to silence this warning
long player1Choice, player2Choice, p, r, s;
                                    ^
                                     = 0
example.c:15:46: warning: variable 'r' is uninitialized when used here
      [-Wuninitialized]
        if((player1Choice=p)&&(player2Choice=r))    
                                             ^
example.c:4:40: note: initialize the variable 'r' to silence this warning
long player1Choice, player2Choice, p, r, s;
                                       ^
                                        = 0
3 warnings generated.