#include stdio.h#include stdlib.h#include string.h#include time.h#include ctype.h
编程学习网为您整理以下代码实例,主要实现:掷两个骰子并呈现总数,然后要求用户猜测下一个总数,希望可以帮到各位朋友。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
int main()
{
int dice1, dice2;
int total1, total2;
time_t t;
char ans;
srand(time(&t));
// give you a number between 0 and 5, so the + 1 makes it 1 to 6
dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total1 = dice1 + dice2;
printf("First roll of the dice was %d and %d, ", dice1, dice2);
printf("for a total of %d.\n\n\n", total1);
do {
puts("Do you think the next roll will be ");
puts("(H)igher, (L)ower, or (S)ame?\n");
puts("Enter H, L, or S to reflect your guess.");
scanf(" %c", &ans);
ans = toupper(ans);
} while ((ans != 'H') && (ans != 'L') && (ans != 'S'));
// Roll the dice a second time to get your second total
dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total2 = dice1 + dice2;
// display the second total for the user
printf("\nThe second roll was %d and %d, ", dice1, dice2);
printf("for a total of %d.\n\n", total2);
if (ans == 'L'){
if (total2 < total1){
printf("%d is lower than %d\n", total2, total1);
}
else
{
printf("Wrong! %d is not lower than %d\n\n", total2, total1);
}
}else if (ans == 'H'){
if (total2 > total1){
printf("%d is higher than %d\n", total2, total1);
}else{
printf("Sorry! %d is not higher than %d\n\n", total2,total1);
}
}else if (ans == 'S'){
if (total2 == total1){
printf("%d is the same as %d\n\n", total2, total1);
}else{
printf("Sorry! %d is not the same as %d\n\n",total2, total1);
}
}
return(0);
}
沃梦达教程
本文标题为:掷两个骰子并呈现总数,然后要求用户猜测下一
基础教程推荐
猜你喜欢
- C++ #define 1970-01-01
- 使用scanf()读取字符串 1970-01-01
- C++输入/输出运算符重载 1970-01-01
- C++按值调用 1970-01-01
- C++定义类对象 1970-01-01
- C语言访问数组元素 1970-01-01
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01
- end() 能否成为 stl 容器的昂贵操作 2022-10-23
- 初始化变量和赋值运算符 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01