2014-10-06 87 views
-2

我正在做一個骰子滾動遊戲! 2個骰子將被滾動併產生兩個1-6之間的隨機數字。總和將取自2個數字並用於確定下一步是什麼。如果用戶的總和是2,3,12,那麼他們會丟失。如果總和是7,11,那麼他們贏。如果總數是4,5,6,8,9,10,那麼程序會再次自動擲骰子直到用戶贏或輸。另外,在顯示每個總和之後,顯示他們已贏/輸的遊戲數量。這是我到目前爲止的代碼:骰子滾動java程序

//import java.util.Scanner; 
public class Lab5 { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 


     //variables 
     int dice1; 
     int dice2; 


     //Call the welcome method 
     welcome(); 

     //fetch random numbers 


     /* 
     * ************************************************************** 
     *welcome method 
     *welcome user 
     *no parameters 
     *no return 
     **************************************************************** 
     */ 
    } 
    public static void welcome() { 
     System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!");{ 
    } 

    int dice1=(int)(Math.random()*6+1); 
    int dice2=(int)(Math.random()*6+1); 
    int sum= dice1 + dice2; 

    System.out.println("Roll: total = " +sum); 

    if (sum==2|| sum==3|| sum==12){ 
    System.out.println("Sorry with a " + sum + " You LOSE :("); } 
    else if(sum==7 || sum==11) { 
    System.out.println("Woah!!! With a " + sum + " You WIN!!!!!!!"); } 
    else{ 
    if(sum==4 ||sum==5 ||sum==6 ||sum==8 ||sum==9 ||sum==10) 
    dice1=(int)(Math.random()*6+1); 
    dice2=(int)(Math.random()*6+1);} 
    int roll2 = dice1 + dice2;} 
    System.out.print("You rolled "+roll2+" ");{ 
    while (roll2 !=7){ 
    if (roll == roll2);{ 
    System.out.println("You Win !"); 
    break; 
    }else{ 

     } 
    } 
}} 

我不知道如何顯示在用戶中贏得/丟失或如何使程序再次擲骰子,如果他們不贏/輸的遊戲。

+1

我可以推薦一個[while循環](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)嗎? – Keppil 2014-10-06 20:15:13

+4

您的標題沒有用。請編輯它以反映手頭的問題。 – 2014-10-06 20:15:40

+0

爲什麼你在主要方法中運行每一件事?啓動一個守護進程線程來運行引擎。 – 2014-10-06 20:51:20

回答

2

你應該使用while循環:一次又一次擲骰子直到玩家贏或輸(然後,break結束while循環)。

while (true) { 
    int dice1=(int)(Math.random()*6+1); 
    int dice2=(int)(Math.random()*6+1); 
    int sum = dice1 + dice2; 

    System.out.println("Roll: total = " + sum); 

    if (sum==2 || sum==3 || sum==12) { 
     System.out.println("Sorry with a " + sum + " You LOSE :("); 
     break; 
    } else if(sum==7 || sum==11) { 
     System.out.println("Woah!!! With a " + sum + " You WIN!!!!!!!"); 
     break; 
    } 

    // If you wanted, you could wait here for the user to confirm (e.g. with a key press) 
}