- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我刚刚开始编写代码,并且正在尝试重现 Monty Hall Problem 游戏节目。我的程序让用户可以在游戏模式、研究模式和退出游戏之间进行选择。我试图简化我的代码并将游戏模式和研究模式放入功能中,但我所做的所有研究对我来说并没有多大意义。我将不胜感激,谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int winning (int win);
float PERCENTAGE (float win,float playedgames);
int RANDOM ();
int main (void)
{
int choice, choice2, Ngames, Ngames2,chosendoor,chosendoor2;
int gamesplayed,gamesplayed2,winningdoor,winningdoor2,revealeddoor;
int switcheddoor, revealeddoor2,stayeddoor, doorchoice,winningdoor3;
int revealeddoor3, choice3, switcheddoor2;
float percent,percent2;
float win = 0,win2 = 0;
int gamesplayed3 = 0,win3 = 0;
srand(time(NULL)); //randomizes based on time
while (choice!=3)
{
printf ("Please enter one of the following options\n\n1. Research mode\n2. Game mode\n3. Exit\n\n");
scanf ("%d", &choice);
switch(choice)
{
case 1:
printf ("\nYou have chosen research mode.\n");
printf ("Please indicate whether you will always us the always- switch strategy or the never-switch strategy.\n");
printf ("1. Always switch doors\n2. Never switch doors\n\n");
scanf ("%d", &choice2);
switch (choice2)
{
case 1:
printf ("\nYou have chosen to always switch doors.\n");
printf ("How many games would you like to play?\n\n");
scanf ("%d", &Ngames);
for (gamesplayed = 0;gamesplayed <= Ngames;gamesplayed++)
{
winningdoor = RANDOM();
chosendoor = 1;
do
{
revealeddoor = RANDOM();
} while (revealeddoor == chosendoor || revealeddoor == winningdoor);
do
{
switcheddoor = RANDOM();
} while (switcheddoor == chosendoor || switcheddoor == revealeddoor);
if (switcheddoor == winningdoor)
{
win = winning(win);
}
}
percent = PERCENTAGE(win,gamesplayed);
printf ("\nYou have chosen the winning door %f percent of the time.\n\n", percent);
win = 0;
break;
case 2:
printf ("\nYou have chosen to never switch doors.\n");
printf ("How many games would you like to play?\n\n");
scanf ("%d", &Ngames2);
for (gamesplayed2 = 0;gamesplayed2 <= Ngames2; gamesplayed2++)
{
winningdoor2 = RANDOM();
chosendoor2 = 1;
do
{
revealeddoor2 = RANDOM();
} while (revealeddoor2 == winningdoor2 || revealeddoor2 == chosendoor2);
do
{
stayeddoor = RANDOM();
} while (stayeddoor != chosendoor2);
if (stayeddoor == winningdoor2)
{
win2 = winning(win2);
}
}
percent2 = PERCENTAGE(win2,gamesplayed2);
printf ("\nYou chose the winning door %f percent of the time.\n\n", percent2);
win2 = 0;
break;
default:
printf ("You have entered an incorrect value.");
break;
}
break;
case 2:
printf ("\nYou have chosen game mode.\n");
winningdoor3 = RANDOM();
printf ("Which door will you choose? (Door 1,2, or 3)\n\n");
scanf ("%d", &doorchoice);
if (doorchoice < 1 || doorchoice > 3)
{
printf ("That value is not a door.\n\n");
break;
}
do
{
revealeddoor3 = RANDOM();
} while (revealeddoor3 == doorchoice);
printf ("\nDoor %d was opened and there was no prize behind it.\n", revealeddoor3);
printf ("Would you like to:\n1. Switch doors?\n2. Stay with your current door?\n\n");
scanf ("%d", &choice3);
switch (choice3)
{
case 1:
printf ("\nYou have chosen to switch doors.\n\n");
do
{
switcheddoor2 = rand()%2 + 1;
} while (switcheddoor2 == doorchoice);
if (switcheddoor2 == winningdoor3)
{
printf ("Good choice! You have won!\n\n");
win3 = winning(win3);
}
else
{
printf ("Unfortunately, you lost.\n\n");
}
++gamesplayed3;
printf ("You have played a total of %d games and you won a total of %d games.\n", gamesplayed3, win3);
break;
case 2:
printf ("\nYou have decided to stay with your current door.\n");
if (doorchoice == winningdoor3)
{
printf ("Good choice! You have won!\n\n");
win3 = winning(win3);
}
else
{
printf ("Unfortunately, you lost.\n\n");
}
gamesplayed3 = gamesplayed3 + 1;
printf ("You have played a total of %d games and you won a total of %d games.\n\n", gamesplayed3, win3);
break;
default:
printf ("You have entered an incorrect value.\n\n");
break;
}
break;
case 3:
printf("Thanks for playing!");
exit(0);
default:
printf ("You have entered an incorrect value.\n\n");
break;
}
}
return 0;
}
int winning (int win)
{
int newwin;
newwin = win + 1;
return (newwin);
}
float PERCENTAGE (float win,float playedgames)
{
float percentage;
percentage = (win/playedgames)*100;
return (percentage);
}
int RANDOM ()
{
int random;
random = rand()%3;
return (random);
}
最佳答案
这是一个示例,用于转换您的一个 switch case...
这是您的代码
case 1:
printf ("\nYou have chosen to always switch doors.\n");
printf ("How many games would you like to play?\n\n");
scanf ("%d", &Ngames);
for (gamesplayed = 0; gamesplayed <= Ngames; gamesplayed++)
{
winningdoor = RANDOM();
chosendoor = 1;
do
{
revealeddoor = RANDOM();
}
while (revealeddoor == chosendoor || revealeddoor == winningdoor);
do
{
switcheddoor = RANDOM();
}
while (switcheddoor == chosendoor || switcheddoor == revealeddoor);
if (switcheddoor == winningdoor)
{
win = winning(win);
}
}
percent = PERCENTAGE(win, gamesplayed);
printf ("\nYou have chosen the winning door %f percent of the time.\n\n", percent);
win = 0;
break;
下面是提取函数后的样子
case 1:
printf ("\nYou have chosen to always switch doors.\n");
printf ("How many games would you like to play?\n\n");
scanf ("%d", &Ngames);
percent = PERCENTAGE(win, kernel(Ngames));
printf ("\nYou have chosen the winning door %f percent of the time.\n\n", percent);
win = 0;
break;
函数如下所示
int kernel (int Ngames)
{
int gamesplayed;
for (gamesplayed = 0; gamesplayed <= Ngames; gamesplayed++)
{
int winningdoor = RANDOM();
int chosendoor = 1;
int revealeddoor,switcheddoor;
do
{
revealeddoor = RANDOM();
}
while (revealeddoor == chosendoor || revealeddoor == winningdoor);
do
{
switcheddoor = RANDOM();
}
while (switcheddoor == chosendoor || switcheddoor == revealeddoor);
if (switcheddoor == winningdoor)
{
win = winning(win);
}
}
return gamesplayed;
}
现在,当您查看特定的 switch case 时,您可以更好地理解其含义。只有当你需要了解内核的逻辑时,你才需要进入内核。
我想说的是,如果您正在计算具有实际意义的东西,请将其作为函数移出。如果您重用某些代码,请将其设为函数。如果您的函数很长或有太多循环(while、for)或条件嵌套(if、switch),那么是时候模块化您的代码了。
关于c - 如何简化这段代码并使其成为函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201560/
我正在学习 Go,但我无法在任何地方找到这个答案。 Web开发中的文件扩展名是否有任何官方标准?我见过多种约定,例如 .tmpl 和 .gtpl,这是什么?谢谢。 最佳答案 没有固定的标准,但有一些相
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 8 年前关闭。 Improve
假设我有两个类(class) Widget ^ | Window 我还有另一个类应用程序: 定义如下 class Application { public: ... private:
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我多年来一直在使用 MySQL,直到去年左右,主要是在较小的项目上。我不确定是语言的性质还是我缺乏真正的教程让我不确定我正在写的东西是否是优化目的和扩展目的的正确方法。 虽然自学 PHP,但我对自己和
我已经多次读到 EJB 是重量级的……但昨晚我正在浏览关于 EJB 的 Java EE 6 教程,它们似乎只是普通的 Java 对象,除了它们可以有像 Stateless 或 Singletons 这
如何使此 SimpleModal 脚本在页面加载时加载而不是单击按钮?谢谢=) Demo 基本模态对话框 对于此演示,SimpleModal 使用此“隐藏”数据作为其内容。您还可以使用标准 HTML
这是 Haskell 中的代码: class Fooable a where foo :: a -> a instance Fooable (a, a) where foo = ...
是否有推荐的方法来测试 Actor 是否使用 be 正确改变了其行为?我更喜欢使用 FSM 的原因之一是因为我可以轻松验证 Actor 是否已更改其行为。我不知道在使用 become/unbecome
我正在构建一个位于“php my admin”中的表,我是在第一次点击其中一个“th”它的 asc 时这样做的,现在我试图在第二次点击时制作 desc ..有什么想法吗? 阿姆..很多我不记得了抱歉.
考虑以下网页。 我在 Firefox 中打开此页面,打开 JS 控制台并键入以下内容。 > document.getElementById(
如何让自己成为 postgresql 的 super 用户? 我一直在尝试创建数据库,但我不断收到以下错误: createdb: database creation failed: ERROR: pe
Query没有太大帮助。 如前所述here , PostgreSQL 是 ORDBMS。 here ,它解释了 PostgreSQL 是 RDBMS。 PostgreSQL 是一个 ORDBMS 是什
我已经看到,当在导航元素中使用的链接中垂直/水平居中文本时,将链接设置为 flex 容器会很有用。我没有意识到链接文本实际上可以是一个(单个) flex 元素。我可以看到链接中的 span 元素可以是
我见过很多说明如何找到给定集合的子集的示例,但是您如何将一个集合设为另一个集合的子集?所以集合 B 是集合 A 的子集,这将如何实现?我目前正在使用递归性质的方案,但是这本书只展示了如何列出子集而不是
有些程序会根据其标准输出是否为 tty 来更改其输出。因此,如果您将它们放入管道或重定向它们,输出将与您的 shell 中的不同。这是一个例子: $ touch a b c # when runnin
我正处于项目的开始阶段,到目前为止我一直在使用默认的 MySQL 数据库。 对了,默认的数据库有名字吗? 我的问题是如何在不删除当前表和创建新表的情况下将现有表更改为 utf-8 和 InnoDB。是
我正在尝试编写一个过滤器来包装数据以遵循 JSON API spec到目前为止,它适用于我直接返回 ActionResult 的所有情况,例如 ComplexTypeJSON。我试图让它在像 Comp
我在 Storyboard 上创建了一个带有一个 UITextField 的自定义 UIViewController。在 viewDidLoad 上,我将 UITextFIeld 设置为 become
我已经看到关于 valueless_by_exception 方法的 cppreference 的以下注释: A variant may become valueless in the followi
我是一名优秀的程序员,十分优秀!