Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Java Program to Toss a Coin

import java.util.Random;
class Toss {
	public String chanceFunc() {
		Random r = new Random();
		int chance = r.nextInt(2);
		if (chance == 1) {
		return"tails";
		} else {
		return"heads";
		}
	}
}
public class Demo {
	public static void main(String[] args) {
		Toss t = new Toss();
		int heads = 0;
		int tails = 0;
		int chances = 10;
		for (int i = 1; i<= chances; i++) {
		if (t.chanceFunc().equals("tails")) {
		tails++;
		  } else {
		heads++;
		  }
		}
		System.out.println("Chances = " + chances);
		System.out.println("Heads: " + heads);
		System.out.println("Tails: " + tails);
		}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.