while lops

Download Raw Clone


  1. double maleRatio = 0.5046500269;
  2. double femaleRatio = 0.4953499731;
  3. int counter = 0;
  4. int numMales = 0;
  5. int numFemales= 0;
  6. Scanner in = new Scanner(System.in);
  7. System.out.print("Number of times to roll? ");
  8. int rollTimes = in.nextInt();
  9. while (counter < rollTimes) {
  10. counter++;
  11. if (Math.random() >= maleRatio) {
  12. numFemales++;
  13. }
  14. else {
  15. numFemales++;
  16. }
  17. }
  18. System.out.println("Males: " + numMales);
  19. System.out.println("Females: " + numFemales);

Raw paste data: