C程序没有退出?

时间:2016-04-27 21:56:54

标签: c loops

我有一项任务,我必须为纸张剪刀摇滚编写一个程序。

你和#34; PC"并且第一个获得2分的胜利。得分2分后,该计划应停止并打印出获胜者。

这是我的代码:

int main()
{
srand(time(NULL));
int dinP = 0;
int pcP = 0;

printf("Welcome to rock paper scissor!\n");

while(dinP != 2 || pcP != 2){

int player, computer, letsgo;

printf("Press any key to continue.\n");
scanf("%d", &letsgo);

printf("Your turn!\n 0 for Paper\n 1 for Rock\n 2 for Scissor\n");
scanf("%d", &player);

computer = rand() % 3;
printf("computers picked",computer);

if (player == 0 && computer == 0 || player == 1 && computer == 1 || player == 2 && computer == 2)

{

//    printf("Player picked %d\n", player);
//    printf("Computer picked %d\n", computer);
    printf("Therefore the result is 0 you ended up equal!\n");

}

if (player == 0 && computer == 1 || player == 1 &&computer == 2 || player == 2 && computer == 0)

{

 //   printf("player picked %d\n", player);
 //   printf("Computer picked %d\n", computer);
    printf("Player wins!\n 1 point for Player\n");
    dinP++;

}


if(player == 0 && computer == 2 || player == 1 && computer == 0 || player == 2 && computer == 1)

{

  //  printf("player picked %d\n", player);
 //   printf("Computer picked %d\n", computer);
    printf("Computer wins!\n Computer Wins! 1 point for the Computer\n");
    pcP++;

}

if(player < 0 || player >= 3)

{
    printf("Please enter a valid number\n In other words, pick either rock, paper or scissor.\n");
}

printf("dinP  : %d - - - - - pcP : %d", dinP, pcP);

}

if(dinP == 2){
    printf("You won\n");
  //  printf("p: %d", dinP);
}else if(pcP == 2){
    printf("Pc Won\n");
  //  printf("p: %d", pcP);
}

return 0;

}

得分2分后,程序不会停止并继续询问我的输入。有什么建议为什么它不会在2分后结束?

1 个答案:

答案 0 :(得分:4)

想想这一部分:

while(dinP != 2 || pcP != 2)

当表达式等于2时,表达式将为false。它将继续运行,直到玩家和计算机都有2分。要解决此问题,只需将||更改为&&