Monty Hall问题Java程序的未知问题

时间:2019-11-21 02:35:45

标签: java

在程序结束时,当它打印出门后的内容时,它将获取默认字符串而不是突变字符串,但是该突变字符串被调用,并且可以与先前方法完美配合。为什么?

此外,对您所选择的门后面的东西的回答总是似乎是不好的奖项(山羊)。这只是一个巧合吗?

如果在我的程序中发现任何其他错误,请在下面评论。

这是第1类

package montehallproblem;

import java.util.Random;

public class GameInfo {
    private boolean reset;

    private String D1;
    private String D2;
    private String D3;

    private String res1;
    private String res2;

    private int choice;

    public GameInfo(){
        res1 ="new car";
        res2 ="goat";
        choice = 404;
        reset = false;

        // NOTE: If un-mutated, the Door Strings will default to this result every time
        D1 = res1;
        D2 = res2;
        D3 = res2;
    }

    public void manualSetChoicesAndDoors(String one, String two, int d1, int d2, int d3){
        setResult1(one);
        setResult2(two);

        //NOTE: This allows the OLC to utilize the previously typed prize strings;
        if(d1==1){
            setDoor1(res1);
        }else if(d1==2){
            setDoor1(res2);
        }else  if(d2==1){
            setDoor2(res1);
        }else if(d2==2){
            setDoor2(res2);
        }else  if(d3==1){
            setDoor3(res1);
        }else if(d3==2){
            setDoor3(res2);
        }
    }

    public void setDoor1(String x){
        D1 = x;
    }

    public void setDoor2(String x){
        D2 = x;
    }

    public void setDoor3(String x){
        D3 = x;
    }

    public void setResult1(String x){
        res1 = x;
    }

    public void setResult2(String x){
        res2 = x;
    }

    public void setChoice(int x){
        choice = x;
    }

    public void setReset(boolean x){
        reset = x;
    }

    public String getDoor1(){
        return D1;
    }

    public String getDoor2(){
        return D2;
    }

    public String getDoor3(){
        return D3;
    }

    public String getResult1(){
        return res1;
    }

    public String getResult2(){
        return res2;
    }

    public int getChoice(){
        return choice;
    }

    public void generatePrizeDoor(){
        if(reset){
            Random rand = new Random();
            int prize = rand.nextInt(3);

            //NOTE: this determines which door has the best prize behind it (res1)
            switch(prize){
            case 1:
                setDoor1(getResult1());
                setDoor2(getResult2());
                setDoor3(getResult2());
                break;
            case 2:
                setDoor1(getResult2());
                setDoor2(getResult1());
                setDoor1(getResult2());
                break;
            case 3:
                setDoor1(getResult2());
                setDoor1(getResult2());
                setDoor1(getResult1());
                System.out.println("a new game generates...");
            }
        }
    }

    public void revealBadDoor(){

        //NOTE: This reveals one of the doors with the bad prize in it;
        //NOTE: Appends "used" to door string so that it can be detected later
        if(getDoor1().contains(getResult2()) && getChoice() != 1){
            System.out.println("Monty reveals door one contains a "+getResult2());
            setDoor1(getDoor1()+"used");
        }else if(getDoor2().contains(getResult2()) && getChoice() != 2){
            System.out.println("Monty reveals door two contains a "+getResult2());
            setDoor2(getDoor2()+"used");
        }else if(getDoor3().contains(getResult2()) && getChoice() != 3){
            System.out.println("Monty reveals door one contains a "+getResult2());
            setDoor3(getDoor3()+"used");
        }
    }

    public void switchDoorChoice(String x){

        //NOTE: This changes your door choice to the door string that doesnt contain "used"
        if(x.contains("yes")){
            switch(getChoice()){
            case 1:
                if(getDoor2().contains("used")){
                    setChoice(3);
                }else if(getDoor3().contains("used")){
                    setChoice(2);
                }
                break;
            case 2:
                if(getDoor3().contains("used")){
                    setChoice(1);
                }else if(getDoor1().contains("used")){
                    setChoice(3);
                }
                break;
            case 3:
                if(getDoor2().contains("used")){
                    setChoice(1);
                }else if(getDoor1().contains("used")){
                    setChoice(2);
                }
                System.out.println("you choose to switch to door "+getChoice());
            }
        }else if(x.contains("no"))
            System.out.println("You choose to stay with door "+getChoice());
        }
    }

    public void revealPlayerDoor(){
        switch(getChoice()){
        case 1:
            System.out.println("Monty reveals your door contains "+getDoor1());
            break;
        case 2: 
            System.out.println("Monty reveals your door contains "+getDoor2());
            break;
        case 3:
            System.out.println("Monty reveals your door contains "+getDoor3());
        }
    }
}

0 个答案:

没有答案