import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;
import java.util.Random;
public class Montyhall
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter number of attempts:");
int attempts = keyboard.nextInt();/*Decide how many times you want this to
run. The larger the number, the more realistic an answer you will get.*/
int curtains[] = new int[3]; //like the game show, one of these will be a winner
Random rand = new Random(); /*sets the methods up with the same random seed, which
otherwise changes by the milisecond and could influence the response*/
withoutswitch(curtains, attempts, rand);
withswitch(curtains, attempts, rand);
}
public static void withoutswitch(int curtains[], int attempts, Random rand)
{
int nsCorrect=0;
for(int x=0; x < attempts; x++)
{
int winner = rand.nextInt(3);//Sets the winner to 1, leaving the other two at 0.
curtains[winner] = 1; //It then checks to see if they are the same,
int guess = rand.nextInt(3); //a 1/3 chance, roughly.
if(curtains[guess]==1){
nsCorrect++;
}
curtains[0]=0;
curtains[1]=0;
curtains[2]=0;
//the player never changes their decision, so it does not matter if a door is opened.
}
System.out.println("Number of successes with no switch: " + nsCorrect);
}
public static void withswitch(int curtains[], int attempts, Random rand)
{
int ysCorrect=0;
int goat = 0;
for(int x=0; x < attempts; x++)
{
int winner = rand.nextInt(3);
curtains[winner] = 1;
int guess = rand.nextInt(3);
goat = rand.nextInt(3);//one of the doors is opened
while(goat == winner || goat == guess)//the opened door is randomized until
goat = rand.nextInt(3); //it isn't the guess or the winner.
int guess2 = rand.nextInt(3);
while(guess2 == guess || guess2 == goat)
guess2 = rand.nextInt(3);//the second guess goes through a similar process
if(curtains[guess2]==1){
ysCorrect++;
}
curtains[0]=0;
curtains[1]=0;
curtains[2]=0;
}
System.out.println("Number of successes with a switch: " + ysCorrect);
}
}
对不起,这有点乱,我正试图在中断了将近一年之后重新开始编程。第一部分按预期运行,返回大约1/3的成功几率。然而,第二种,应该有2/3的机会,但我仍然得到大致相同的数量的正确开关和没有开关。我浏览了整个网站,发现了一些我不熟悉的Java之外的东西。这一个非常相似,但似乎没有人真正帮助解决主要问题。
我该怎么做才能让这种可能性更现实?对于清理代码的建议也很感激。
编辑:代码现在功能,我现在只是试图精简它。
在您的方法withoutswitch
中,您需要更改
if(guess==1)
nsCorrect++;
if (curtains[guess] == 1)
nsCorrect++;
与withswitch
方法相同。每次运行for循环后,您需要将curtains
重置为0。否则,之前的1
将在那里,运行几次后,curtains
将只包含1。
private static void resetCurtains(int[] curtains) {
for (int i = 0; i < curtains.length; i++) {
curtains[i] = 0;
}
}
在for循环内每次运行后调用该方法。
此外,我建议使用{}
,即使语句是1-liner:
if (curtrains[guess] == 1) {
nsCorrect++;
}
你的两个方法都不能正常工作
你的"withoutswitch"方法只从0、1或2中随机选择一个数字,并在其为1时将其添加到计数器中。换句话说,我可以将"withoutswitch"方法简化为:
public static void withoutswitch(int attempts, Random rand) {
int counter = 0;
for (int i = 0; i < attempts; i++) {
if (rand.nextInt(3) == 1) counter++;
}
System.out.println(counter);
}
你的"withswitch"方法,信不信由你,做了完全相同的事情。你开始用一个变量猜测,然后你完全忽略它,对第二个变量进行猜测,并检查它是否为1。因此,它产生完全相同的结果。
你的两个方法都使用"窗帘"数组,但不正确。这个问题的要点是每次都把汽车放在一个随机的门后面,但你永远不会把你的数组设置回全0,所以,几次运行后,它变成了一个全1的数组,这绝对不是你想要的。
这里有一些伪代码来帮助你开始:
number of switch wins = 0
number of stay wins = 0
scan in the number of attempts
loop through each attempt:
make an array {0, 0, 0}
pick a random number (0, 1, or 2), and set that array index to 1 (winning index)
pick a random number (0, 1, or 2), and set it to choice
pick a random number (0, 1, or 2), and set it to the shown door
loop while the door number isn't the choice number or the winning index:
pick a random number (0, 1, or 2) and set it to the shown door
increment stay wins if the choice is equal to the winning index
increment switch wins if (3 - choice - showndoor) is equal to the winning index
print stay wins and switch wins
注意:最后一位逻辑决定了被切换的门的索引,因为你唯一可能的索引是0,1和2,(0 + 1 + 2 = 3),那么3 -你选择的门-你显示的门=最后一扇门。
希望这对你有帮助!
我会备份并重新构建整个内容。游戏中的一个对象,两个后代,其中一个简单地选择一扇门,其中一个选择然后切换。
我不完全遵循你的切换逻辑,但这绝对是错误的。Monty的逻辑是,如果奖品在玩家的门后面,你就随机打开一扇,否则你就用山羊打开一扇。只需要一个随机数
同样地,玩家也会切换。此时只有一个幕布,不需要随机数。对逻辑的粗略描述(不是Java,也不是整个程序):
MontyHaulGame
{
int[] Curtains = new int[3];
int Car = Random(3);
int Guess;
Pick();
if (Guess == Car) Wins++;
}
MontyHaulNoSwitch : MontyHaulGame
{
Pick()
{
Guess = Random(3);
}
}
MontyHaulSwitch : MontyHaulGame
{
Pick()
{
Guess = Random(3);
OpenOne();
Switch();
}
OpenOne()
{
if Guess == Car then
Repeat
Monty = Random(3);
Until Monty != Guess;
else
Monty = 1;
While (Monty == Guess) || (Monty == Car)
Monty++;
}
Switch()
{
NewGuess = 1;
While (NewGuess == Guess) || (NewGuess == Monty)
NewGuess++;
Guess == NewGuess;
}
}