Advertisement
Guest User

TwoDiceSum

a guest
Oct 22nd, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. public class TwoDiceSum {
  5.  
  6.  
  7. public static void main(String[] args) {
  8. double count = 0.0;
  9. int rollvalue = 0;
  10. double dice1 = 0.0;
  11. double dice2 = 0.0;
  12. Scanner input = new Scanner(System.in);
  13. int desiredSum = input.nextInt();
  14. long numberRolls = input.nextInt();
  15.  
  16. for (int i = 0; i <= numberRolls; i++) {
  17. dice1 = (int)(Math.random()*6) + 1;
  18. dice2 = (int)(Math.random()*6) + 1;
  19. rollvalue = (int) (dice1 + dice2);
  20. if (rollvalue == desiredSum) {
  21. count++;
  22. }
  23.  
  24. }
  25.  
  26.  
  27. double unroundedprobability = count / (numberRolls);
  28. double probability = (int) (unroundedprobability * 100.0) / 100.0;
  29. System.out.println();
  30. System.out.println("Enter desired sum of two dice: Enter number of rolls: probability: " + probability);
  31.  
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement