2015-11-04 72 views
1
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); 




    } 
} 

我正在制作蒙蒂厅游戏节目。我是新来的,我只是想知道为什么最后打印出来的时候,为什么总是打印出0.我想打印出值3,因为我认为他们会等于 - This Part试图让价值观等于

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

回答

1

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

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

哦哇感谢:)现在我觉得哑巴。 –

+0

我有另一个问题...当我随机化的门1,门2和门3的值我不断重复数字,所以我把那些如果在那里重新随机化他们,但它没有奏效。任何建议@Flying_Banana –

+0

因为你再次随机化,并且由于新值可以是1-3之间的任何值,所以你有33%的机会获得相同的值。相反,你应该在1-3之间随机选择,然后在1-2之间随机选择(或者1 3或2 3,这取决于第一个门的号码是什么),最后门将会有剩余数字。 –