- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这段代码的用途:模拟100场CRAPS,记录第一轮输,第一轮赢,第二轮负加分,第二轮赢加分的#。
那些不熟悉掷骰子规则的人;您基本上掷两个骰子,如果结果不是 2、3 或 12 的总数,您可以再次掷骰(您在该轮掷的数字将保留并添加到您的积分中)。如果您掷出 7 或 11,您将自动获胜。
这是我目前所处的位置:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main ()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times.\n");
for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
if (sumd==7 || sumd==11) {
printf("You rolled a 7 or an 11, you win.\n");
winf++;
}
if (sumd==2 || sumd==3 || sumd==12) {
printf("You rolled a 12, a 3, or a 2, you lose.\n");
lostf++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
while (1) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;
if (sumd2==sumd){
printf("You rolled your points, you win.\n");
winp++;
break;}
if (sumd==7){
printf("You rolled a 7, you lose.\n");
lostp++;
break;}
}
}
}
printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. ", winf, lostf, winp, lostp);
}
我只要求你给我一些选择,让我知道如何保留这些要在最后打印的点??
此外,我觉得我的代码可以写得更好,减少冗余,有什么建议吗?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main ()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times. Press any key to continue.\n");
//getchar();
for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
switch(sumd){
case 7:
case 11:
printf("You rolled %d, you win.\n", sumd);
winf++;
break;
case 2:
case 3:
case 12:
printf("You rolled %d, you lose.\n", sumd);
lostf++;
break;
default:
while (1) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;
if (sumd2==sumd){
printf("You rolled your points(%d), you win.\n",sumd);
winp++;
break;}
if (sumd2==7){
printf("You rolled a 7, you lose.\n");
lostp++;
break;}
}
}
}
printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. \n", winf, lostf, winp, lostp);
}
最佳答案
你可以很容易地压缩
的两次出现d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;
进入一个函数:
int rolldice(){
int d1,d2;
d1 = rand()%6+1;
d2 = rand()%6+1;
return d1 + d2;
}
或者单行形式:
int rolldice(){
return (rand()%6)+(rand()%6)+2;
}
然后你会写
sumd = rolldice();
关于C:掷骰子/骰子游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9189471/
我刚刚向自己介绍了 WebGL 和 Threejs,作为开始,我正在尝试创建一个 3D 骰子。我已经到了创建立方体的地步,但我真的不知道如何在立方体的面上放置数字,我能找到的只是如何更改颜色。我已经查
这就是我现在得到的...... var max = 7; var min = 1; $('#dice').click(function() { random = Math.floor(Math.r
我是 Unity 的新手,一直在尝试掷骰子。我遇到了一组教程,它们允许我创建一个 3d 模具(模具使用 Rigidbody 和 Mesh Collider)并编写脚本使其在空格键上滚动,如下所示: 骰
我是 JavaScript 新手,我需要一些想法/帮助来了解如何使我的脚本正常工作。所以我们的想法是,你掷两个骰子,通过按下“掷骰子”按钮然后按下重置按钮来重置整个 HTML 中的所有内容。 所以事情
我正在尝试构建一个掷骰子游戏,如果计算机自动掷出一对骰子,并且如果 cpu 掷出 7 或 11,则用户获胜。然而,如果用户掷出 2、3 或 12,他们将自动失败。如果用户滚动任何其他数字(4、5、6、
背景 如此处所述http://www.ericharshbarger.org/dice/#gofirst_4d12 , “先走”骰子是一组四个骰子,每个都有唯一的编号,因此: 任何两个或更多骰子都不会
我是一名优秀的程序员,十分优秀!