Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Lab04
  4. {
  5. public static void main(String[] args) throws IOException
  6. {
  7. int firstArg = 0;
  8. if (args.length > 0)
  9. {
  10. try
  11. {
  12. firstArg = Integer.parseInt(args[0]);
  13. } catch (NumberFormatException e) {
  14. //System.err.println("Argument" + args[0] + " must be an integer.");
  15. System.out.println("enter number of times to flip coin.");
  16. System.exit(1);
  17. }
  18. }
  19.  
  20. int FLIPS = firstArg;
  21. int count = 0;
  22. int nheads = 0;
  23. int ntails = 0;
  24.  
  25. Coin coin = new Coin();
  26.  
  27. if(firstArg <= 0)
  28. {
  29.  
  30. System.out.println("Please enter the number of times to flip the coin.");
  31. InputStreamReader isr = new InputStreamReader( System.in );
  32. BufferedReader stdin = new BufferedReader(isr);
  33. //String buff = stdin.readLine(); // clear input buffer from earlier read
  34. String temp = stdin.readLine();
  35. FLIPS = Integer.parseInt(temp);
  36.  
  37. }
  38.  
  39. while(count < FLIPS)
  40. {
  41. coin.toss();
  42. if(coin.isHeads())
  43. {
  44. nheads++;
  45. System.out.println(coin);
  46. }
  47. else
  48. {
  49. ntails++;
  50. System.out.println(coin);
  51. }
  52. count++;
  53.  
  54. }
  55.  
  56. System.out.println("The number of tosses: " + FLIPS);
  57. System.out.println("The number of heads: " + nheads);
  58. System.out.println("The number of tails: " + ntails);
  59.  
  60. Compute compute = new Compute();
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement