Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Lab04
- {
- public static void main(String[] args) throws IOException
- {
- int firstArg = 0;
- if (args.length > 0)
- {
- try
- {
- firstArg = Integer.parseInt(args[0]);
- } catch (NumberFormatException e) {
- //System.err.println("Argument" + args[0] + " must be an integer.");
- System.out.println("enter number of times to flip coin.");
- System.exit(1);
- }
- }
- int FLIPS = firstArg;
- int count = 0;
- int nheads = 0;
- int ntails = 0;
- Coin coin = new Coin();
- if(firstArg <= 0)
- {
- System.out.println("Please enter the number of times to flip the coin.");
- InputStreamReader isr = new InputStreamReader( System.in );
- BufferedReader stdin = new BufferedReader(isr);
- //String buff = stdin.readLine(); // clear input buffer from earlier read
- String temp = stdin.readLine();
- FLIPS = Integer.parseInt(temp);
- }
- while(count < FLIPS)
- {
- coin.toss();
- if(coin.isHeads())
- {
- nheads++;
- System.out.println(coin);
- }
- else
- {
- ntails++;
- System.out.println(coin);
- }
- count++;
- }
- System.out.println("The number of tosses: " + FLIPS);
- System.out.println("The number of heads: " + nheads);
- System.out.println("The number of tails: " + ntails);
- Compute compute = new Compute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement