當前位置:編程學習大全網 - 編程語言 - 用C語言函數做猜拳問題,出現個錯誤不知道改``高手指點

用C語言函數做猜拳問題,出現個錯誤不知道改``高手指點

妳出的問題真是不少。

1、函數choiceuser寫錯了,妳並沒有用到那兩個參數a\b為什麽要寫呢?

2、函數聲明壹次就夠了,為什麽還要在main裏再聲明壹次呢?

3、函數中如果執行了return命令,就不會再往下執行了,所以“您出的是:石頭”這些話並不會輸出。

4、這個不算錯誤,只是編程習慣而已,為什麽if後面只有壹條語句妳還要加大括號呢?這樣會使程序可讀性降低的。

我給妳改好了:

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int choiceUser(void);

int choiceComputer(void);

void main()

{

do

{

int a=1,b=2,c=3,d,e;

printf("請出拳<1-石頭;2-剪子;3-布>:");

d=choiceUser();

e=choiceComputer();

if(d==1&&e==2||d==2&&e==1||d==3&&e==1)

printf("您贏了。");

else if(d==1&&e==3||d==2&&e==1||d==3&&e==2)

printf("您輸了。");

else

printf("您和電腦打平。");

}while(1);

}

int choiceUser()

{

int user;

scanf("%d",&user);

if(user>=1&&user<=3)

{

if(user==1)

printf("您出的是:石頭\n");

if(user==2)

printf("您出的是:剪刀\n");

if(user==3)

printf("您出的是:布\n");

}

else

printf("您輸入錯誤!\n");

return user;

}

int choiceComputer()

{

int computer;

srand((unsigned)time(NULL));

computer=rand()%3+1;

if(computer==1)

printf("電腦出的是:石頭\n");

if(computer==2)

printf("電腦出的是:剪刀\n");

if(computer==3)

printf("電腦出的是:布\n");

return computer;

}

  • 上一篇:程序員學哪種語言最好
  • 下一篇:自己做的軟件怎麽才能出現在微信小程序
  • copyright 2024編程學習大全網