2014-09-30 52 views
0

我想做一個骰子,不斷拋出,直到它擊中6: 當它擊中1時,它彈出1和一個眼睛的骰子,並再次拋出,.. 只是隨機數字(明顯1-6)直到命中6.當命中6 它應該停止。問題與我的Java循環

現在我有了這個開關,它顯示了正確的數字時,但我有麻煩讓這個開關正常工作。或者它擊中所有的數字,但六,並繼續生成數字,或者它一直拋出相同的數字。

任何人都可以借我一把嗎?

非常讚賞


public static void main(String[] args) { 

    // asking what symbol to use to print the eye(s) of the dice 
    System.out.print("choose symbol to use for eyes: "); 

    char ch; 
    Scanner sc = new Scanner(System.in); 

    ch = sc.findInLine(".").charAt(0); 
    int dice = (int)(6*Math.random()) + 1; 


    do{ 
     switch(dice % 6){ 
      case 0: System.out.println("1"); 
        System.out.println(ch); 
      break; 
      case 1: System.out.println("2"); 
        System.out.println(ch + "\n\n " + ch); 
      break; 
      case 2: System.out.println("3"); 
        System.out.println(ch + "\n " + ch + "\n " + ch); 
      break; 
      case 3: System.out.println("4"); 
        System.out.println(ch + " " + ch + "\n" + ch + " " + ch); 
      break; 
      case 4: System.out.println("5"); 
        System.out.println(ch + " " + ch + "\n" + " " + ch + " \n"+ ch + " " + ch); 
      break; 
     } 
    } 
    while(dice < 6); 
     // Else{ System.out.println("6"); 
     //   System.out.println(ch + " " + ch + "\n" + ch + " " + ch + "\n" + ch + 
     //  " " + ch); 
     } 
    } 

} 
+2

因爲你只是隨機產生一次數字。 – proulxs 2014-09-30 19:41:12

+0

你的循環條件是錯誤的,它應該是骰子%6 == 0。在循環中生成隨機數/ – StackFlowed 2014-09-30 19:41:14

+0

還有另一個需要涉及的問題。你的骰子值將是1到6.然後你取這個模數。 1%6 = 1,但你有設置爲案例0. – Compass 2014-09-30 19:44:37

回答

0

你產生循環外面的隨機數,所以這將是永遠一樣這裏的工作版本:

public static void main(String[] args) { 

    // asking what symbol to use to print the eye(s) of the dice 
    System.out.print("choose symbol to use for eyes: "); 

    char ch; 
    Scanner sc = new Scanner(System.in); 

    ch = sc.findInLine(".").charAt(0); 
    int dice = (int)(6*Math.random()) + 1; 


    do{ 
     switch(dice % 6){ 
      case 0: System.out.println("1"); 
        System.out.println(ch); 
      break; 
      case 1: System.out.println("2"); 
        System.out.println(ch + "\n\n " + ch); 
      break; 
      case 2: System.out.println("3"); 
        System.out.println(ch + "\n " + ch + "\n " + ch); 
      break; 
      case 3: System.out.println("4"); 
        System.out.println(ch + " " + ch + "\n" + ch + " " + ch); 
      break; 
      case 4: System.out.println("5"); 
        System.out.println(ch + " " + ch + "\n" + " " + ch + " \n"+ ch + " " + ch); 
      break; 
     } 
     dice = (int)(6*Math.random()) + 1; 
    } 
    while(dice < 6); 
    System.out.println("6"); 
     //   System.out.println(ch + " " + ch + "\n" + ch + " " + ch + "\n" + ch + 
     //  " " + ch); 
} 

here是在線工作版本。

+0

這是工作,但現在它不顯示6時,如何執行「如果骰子= 6然後顯示六停止」?你能告訴我爲什麼我必須把骰子放在循環中和上面。我可以看到它與他們在那裏工作,而不是當我刪除一個,..不明白爲什麼。謝謝你的回覆 – Rick 2014-09-30 20:10:39

+0

@Rick如果你想展示六隻眼睛,只需從我的代碼中移除兩行最後一行註釋,你不必把骰子放在循環之上,我只是希望它看起來更像你的代碼,你可以簡單地把int dice =(int)(6 * Math.random())+ 1;在你的循環內部,它會工作,因爲每次迭代它都會產生另一個隨機數。我希望這個幫助。請如果它你想要標記這個答案。 – Lrrr 2014-09-30 20:27:23

+0

謝謝!所以基本上......你可以只說出在這段時間之後沒有提到的條件時應該發生什麼?所以雖然(骰子<6);和下一行說當它大於5時會發生什麼? – Rick 2014-09-30 20:41:35

2

您需要將循環內的以下內容:

int dice = (int)(6*Math.random()) + 1; 

(否則你有效地扔模具只有一次)

而且,您生成隨機數的方式,您打開它的方式,以及條形碼while離子不完全一致。

+0

感謝您的答覆。你會如何建議沒有開關呢? – Rick 2014-09-30 20:17:16