fork download
  1. class HeadsTails
  2. {
  3. public static void main(String[] args)
  4. {
  5. int v;
  6. double i;
  7.  
  8. int heads = 0, tails = 0;
  9.  
  10. double headsPercent, tailsPercent;
  11.  
  12. for(v = 1; v <= 10; ++v)
  13. {
  14. i = Math.random();
  15. if(i <= 0.5)
  16. {
  17. System.out.println("Heads");
  18. heads = heads + 1;
  19. }
  20. else if(i > 0.5)
  21. {
  22. System.out.println("Tails");
  23. tails = tails + 1;
  24. }
  25. }
  26. headsPercent = ((double)heads / 10) * 100;
  27. tailsPercent = ((double)tails / 10) * 100;
  28. System.out.println("Heads were " + headsPercent + "% of the tosses.");
  29. System.out.println("Tails were " + tailsPercent + "% of the tosses.");
  30. }
  31. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Tails
Tails
Heads
Heads were 80.0% of the tosses.
Tails were 20.0% of the tosses.