C语言小白的第八课:循环语句之do语句

do语句

do{

}while (a == 0)

1、如果变量a不为0,那么就跳出循环,否则一直循环。

用do语句和判断语句写了一个石头剪刀布的小游戏.

do语句在这里的作用一是当玩家输入不符合规范时提醒再次输入,如此循环直到符合要求;二是可以让玩家反复游玩。另外,为了不让下一个玩家看到上一个玩家出的拳,使用了system("cls")的清屏操作,又为了得到更好效果使用了system("pause")的暂停操作。

#include <stdio.h>
#include <stdlib.h>
int hand1=0,hand2=0;
int main() {
	int a = 0;
	do {
		printf("我们来玩石头剪刀布吧!!!\n");
		system("pause");
		system("cls");
		do {
			printf("请输入玩家一想出的拳:【剪刀...0/石头...1/布...2】\n");
			scanf("%d", &hand1);
			if (hand1 < 0 || hand1 > 2) { printf("请注意,请输入0,1,2中的一个数\n"); }
		} while (hand1 < 0 || hand1 > 2);
		system("cls");
		do {
			printf("请输入玩家二想出的拳:【剪刀...0/石头...1/布...2】\n");
			scanf("%d", &hand2);
			if (hand2 < 0 || hand2>2) { printf("\n请注意,请输入0,1,2中的一个数\n"); }
		} while (hand2 < 0 || hand2>2);
		system("cls");
		printf("让我们来看看结果吧...\n"); system("pause"); system("cls");
		int result = 0;
		if (hand1 == hand2) { result = 0; };
		if (hand1 == 1 && hand2 == 0) { result = 1; };
		if (hand1 == 0 && hand2 == 1) { result = 2; };
		if (hand1 == 1 && hand2 == 2) { result = 2; };
		if (hand1 == 2 && hand2 == 1) { result = 1; };
		if (hand1 == 0 && hand2 == 2) { result = 1; };
		if (hand1 == 2 && hand2 == 0) { result = 2; };

		switch (result) {
		case 0: printf("平局!!!\n");  break;
		case 1: printf("玩家一赢了!!!\n");  break;
		case 2: printf("玩家二赢了!!!\n");  break;
		}
		system("pause");
        system("cls");
		printf("想再来一把吗?【想...0/不想...1】\n\n");
		scanf("%d", &a);
	} while (a == 0);
	return 0;
}

【补】1、德摩根定律:x && y = !(!x ||!y)    x || y = !(!x && !y)        2、复合赋值运算符 sum +=c与sum = sum + c的作用一样,前者被称为复合赋值运算符。复合赋值运算符一共有十个,为         (1)*=  (2)/=  (3)%=  (4)+=   (5)-=  (6)<<= (7)>>=  (8)&=  (9)^=   (10) |=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值