试图让价值相等

时间:2015-11-04 02:04:07

标签: java

import java.util.Random;
import java.util.Scanner;
public class javascript {
    public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();

int door1 = 0;
int door2 = 0;
int door3 = 0;
int gdoor1 = 0;
int gdoor2 = 0;
int gdoor3 = 0;
int cdoor = 0;
door1 = rand.nextInt(3)+1;  
door2 = rand.nextInt(3)+1;  
door3 = rand.nextInt(3)+1;  

if (door1 == door2 || door1 == door3){
    door1 = rand.nextInt(3)+1;
}
if (door2 == door1 || door2 == door3){
    door2 = rand.nextInt(3)+1;
}
if (door3 == door1 || door3 == door2){
    door3 = rand.nextInt(3)+1;
}

System.out.println("Choose A Door");
System.out.println(" _____      _____       _____ ");
System.out.println("|     |    |     |     |     |");
System.out.println("|  1  |    |  2  |     |  3  |");
System.out.println("|     |    |     |     |     |");
System.out.println("|     |    |     |     |     |");
System.out.println("|_____|    |_____|     |_____|");
System.out.println("\n");


int yourdoor = scan.nextInt();
System.out.println("Your Door: "+yourdoor); 

if (door1 == 1 || door1 == 2){
    door1 = gdoor1;}
if (door2 == 1 || door1 == 2){
    door2 = gdoor2;}
if (door3 == 1 || door1 == 2){
    door3 = gdoor3;}

if(door1 == 3){
    door1 = cdoor;}
if(door2 == 3){
    door2 = cdoor;}
if(door3 == 3){
    door3 = cdoor;}

System.out.println("Value = "+cdoor);




    }
}

我正在制作Monty Hall游戏节目。我是新手,我只是想知道为什么最后我打印出cdoor为什么它总是打印出来0.我希望它打印出值3因为我认为它们会相等因为 - 这部分< / p>

    if(door1 == 3){
        door1 = cdoor;}
    if(door2 == 3){
        door2 = cdoor;}
    if(door3 == 3){
        door3 = cdoor;}

1 个答案:

答案 0 :(得分:1)

因为cdoor始终为0.它永远不会被修改。也许你的意思是,

if (door1 == 3) {
    cdoor = door1;
}
if (door2 == 3) {
    cdoor = door2;
}
if (door3 == 3) {
    cdoor = door3;
}