C语言编程二维数组
#include#include#includevoid game1(){ int temp=0; int guess; int count=3; bool judge=false; srand((unsigned)time(NULL)); temp=rand()%10; while((count>0)&&(judge==false)) { printf("请输入所猜一维数组下标(1~10):"); scanf("%d",&guess); if(guess==temp) { printf("\n恭喜你!猜对了"); judge=true; } else printf("\n很遗憾猜错了,还有%d次机会\n",count-1); count--; }} void game2(){ int temp1=0; int temp2=0; int guess1; int guess2; int count=3; bool judge=false; srand((unsigned)time(NULL)); temp1=rand()%10; temp2=rand()%10; while((count>0)&&(judge==false)) { printf("请输入二维数组的行下标(1~10):"); scanf("%d",&guess1); printf("\n请输入二维数组的列下标(1~10):"); scanf("%d",&guess2); if((guess1==temp1)||(guess2==temp2)) { printf("\n恭喜你!猜对了"); judge=true; } else printf("\n很遗憾猜错了,还有%d次机会\n",count-1); count--; }}void main(){ int opt=-1; char again='Y'; printf(" 猜数组下标游戏 \n"); printf(" 1.一维数组 \n"); printf(" 2.二维数组 \n"); printf(" 0.退出 \n"); while((opt!=0||opt!=1||opt!=2)&&(again=='Y')) { printf(" 请输入游戏序号:"); scanf("%d",&opt); if(opt==1) { printf("您选择了1\n"); game1(); printf("是否继续(Y/N)?"); getchar(); scanf("%c",&again); } else if(opt==2) { printf("您选择了2\n"); game2(); printf("是否继续(Y/N)?"); getchar(); scanf("%c",&again); } else if(opt==0) exit(0); else printf("非法输入!请重新输入序号\n"); } }