Advertisement
Guest User

TwoDiceSum

a guest
Oct 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. package apcs;
  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. int numberRolls = input.nextInt();
  15.  
  16. for (int i = 0; i <= numberRolls; i++) {
  17. dice1 = (Math.random()*6) + 1;
  18. dice2 = (Math.random()*6) + 1;
  19. rollvalue = (int) (dice1 + dice2);
  20. if (rollvalue == desiredSum) {
  21. count++;
  22. }
  23.  
  24. }
  25.  
  26.  
  27. double unroundedprobability = count / (double)(numberRolls);
  28. double probability = Math.round(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