gpt4 book ai didi

c - 我的 Do-While 循环似乎没有循环?

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:14 26 4
gpt4 key购买 nike

请帮忙,当我要求用户输入 continue using scanf 和 printf 时,我似乎无法让 do-while 循环循环。可能是 wipe_buffer 吗?我似乎无法在这方面做对,我只需要完成循环,这应该主要是功能性的。编码新手和这个网站请不要太苛刻。请谢谢。

    #include <stdio.h>
#include <stdlib.h>
void wipe_buffer(void);
int main(int argc, char* argv[])

{
char play1;
char play2;
char cont;

do{
printf("Player one pick Rock, Paper, or Scissors\n");
scanf(" %c", &play1);
wipe_buffer();
printf("Player two pick Rock, Paper, or Scissors\n");
scanf(" %c", &play2);
wipe_buffer();

switch(play1)
{
case 'r':
if(play2 == 'p' || play2 == 'P')
{
printf("Paper Covers Rock\n");
printf("Player two wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Rock Breaks Scissors\n");
printf("Player one wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Draw, Nobody wins\n");
}
break;
case 'R':
if(play2 == 'p' || play2 == 'P')
{
printf("Paper Covers Rock\n");
printf("Player two wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Rock Breaks Scissors\n");
printf("Player one wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Draw, Nobody wins\n");
}
break;
case 'P':
if(play2 == 'p' || play2 == 'P')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Scissors cuts Paper\n");
printf("Player two wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Paper covers rock\n");
}
break;
case 'p':
if(play2 == 'p' || play2 == 'P')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Scissors cuts Paper\n");
printf("Player two wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Paper covers rock\n");
}
break;
case 's':
if(play2 == 'p' || play2 == 'P')
{
printf("Scissors Cuts Paper\n");
printf("Player one wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Rock breaks Scissors\n");
printf("Player two wins\n");
}
break;
case 'S':
if(play2 == 'p' || play2 == 'P')
{
printf("Scissors Cuts Paper\n");
printf("Player one wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Rock breaks Scissors\n");
printf("Player two wins\n");
}
break;
}
printf("Do you wish to continue?\n");
scanf(" &c", &cont);
wipe_buffer();
}while(cont == 'y' || cont == 'Y');
}
void wipe_buffer(void)
{
char t;
scanf("%c", &t);
while(t != '\n' )
{
scanf("%c", &t);
}
return;
}

最佳答案

在本节中:

    printf("Do you wish to continue?\n");
scanf(" &c", &cont);

"&c" 应该是 "%c"

请注意,当我使用 gcc 编译它时,我收到一条基本上解决了这个问题的警告,因此请务必阅读您的警告。

关于c - 我的 Do-While 循环似乎没有循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19214643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com