2014-10-03 37 views
-2

我對Java很陌生,目前有一個處理循環和隨機數的任務。問題如下:修改ThrowingPairsOfDice.java進行計數,並將這對數相加爲2,總和爲3,總和爲4,總和爲5,總和爲6.顯示結果。循環中的隨機數和計數發生

這是需要修改的代碼:

import java.util.Random; 
import java.util.Scanner; 

public class ThrowingPairsOfDice 
{ 

/** 
* main method simulates throwing 2 dice 1,000,000 times 
* 
*/ 
    public static void main(String[] args) 
    { 
     Random die1 = new Random(); 
     Random die2 = new Random(); 
     int throwOfDice=0; 

     System.out.println("How many times should I throw two dice?"); 
     Scanner kb = new Scanner(System.in); 

     int numberOfTimes = kb.nextInt(); 

     // throw the dice the number of times requested 

     for (int i=1;i<=numberOfTimes;i++) 
     { 
      int toss1 =die1.nextInt(5)+1; 
      int toss2 =die2.nextInt(5)+1; 
      throwOfDice = toss1 + toss2; 
      System.out.println(toss1+ "+" +toss2+ "= " +throwOfDice); 
     } 

    } 
} 

我不知道如何修改這個程序來計算時代的一對骰子將總結爲給定值的數量,從而幫助將不勝感激!謝謝。

+0

您是否瞭解您發佈的代碼?總結給定的值? – 2014-10-03 01:37:18

+1

Elliot提出的問題依然存在,你是否理解你發佈的代碼? – 2014-10-03 01:44:58

+2

@jprez我會*強烈*建議你坐下來,考慮一下,可能會去你的電訊管理局的辦公時間提問。你在這裏得到的答案是使用一些比你可能已經介紹的更高級的概念(地圖和切換是這裏的兩個例子),這可能會導致你錯過教授想要教的目標。與我們所知的Java庫的內部結果相比,您所展示的約束可能會導致您應該學習和寫作的內容與您在此獲得的答案之間的重大不匹配。 – 2014-10-03 02:23:20

回答

0

你只新Random實例來生成兩個隨機int值,你可以使用printf()得到格式化輸出和ifelse if總結你的骰子。類似的,

Random die = new Random(); // <-- only need one. 
// The number of sides to the die. 
int sides = 6; 
// Variables to count the results 
int rolled2 = 0; 
int rolled3 = 0; 
int rolled4 = 0; 
int rolled5 = 0; 
int rolled6 = 0; 

// throw the dice the number of times requested 
for (int i = 1; i <= numberOfTimes; i++) { 
    int toss1 = 1 + die.nextInt(sides); 
    int toss2 = 1 + die.nextInt(sides); 
    int throwOfDice = toss1 + toss2; 
    System.out.printf("%d + %d = %d%n", toss1, toss2, throwOfDice); 
    if (throwOfDice == 2) { 
     rolled2++; 
    } else if (throwOfDice == 3) { 
     rolled3++; 
    } else if (throwOfDice == 4) { 
     rolled4++; 
    } else if (throwOfDice == 5) { 
     rolled5++; 
    } else if (throwOfDice == 6) { 
     rolled6++; 
    } 
} 
System.out.printf("2 = %d, 3 = %d, 4 = %d, 5 = %d, 6 = %d%n", rolled2, 
     rolled3, rolled4, rolled5, rolled6); 
1

您需要使用額外的變量來存儲#總數。在我的答案中,我使用數組sum []並使用die減去兩個之和來引用它。

import java.util.Random; 
import java.util.Scanner; 

public class ThrowingPairsOfDice 
{ 

/** 
* main method simulates throwing 2 dice 1,000,000 times 
* 
*/ 
    public static void main(String[] args) 
    { 
     Random die1 = new Random(); 
     Random die2 = new Random(); 
     int throwOfDice=0; 
     int [] sum = {0, 0, 0, 0, 0}; 


     System.out.println("How many times should I throw two dice?"); 
     Scanner kb = new Scanner(System.in); 

     int numberOfTimes = kb.nextInt(); 

     // throw the dice the number of times requested 

     for (int i=1;i<=numberOfTimes;i++) 
     { 
      int toss1 =die1.nextInt(5)+1; 
      int toss2 =die2.nextInt(5)+1; 
      throwOfDice = toss1 + toss2; 
      System.out.println(toss1+ "+" +toss2+ "= " +throwOfDice); 
      if (throwOfDice < 7) sum[throwOfDice - 2]++; 
     } 

     for (int i=0;i<=4;i++) 
     { 
      System.out.println("# of throws totalling " + (i + 2) + " = " + sum[i]); 
     } 
    } 
} 
+0

如果'switch'已經引入學生,我會感到驚訝......在這種情況下,它是完全不必要的。 'sum [throwOfDice - 2] ++;'不需要任何分支就可以工作......並且還處理throwOfDice大於6的情況(儘管我注意到這是寫在問題中的要求)。 – 2014-10-03 02:10:08

+0

由於不需要龐大的'switch'語句來完成賦值,我將它移除並用'sum [throwOfDice-2] ++;'替換它以及if語句,以將存儲的die總數限制爲2-6 – Ganeshvara 2014-10-03 02:33:44