Coin Loop Selective Print Statement Problem
This is a homework assignment and I am stuck and I have researched every book I know that talks about loops.
I am required to print the loop at every 25th loop up to 500, i.e. 1-25th, 26-75th, 100th, 125th, etc.
I guess another way of stating my problem is that I want to sum up the toss results for every 25 loop iterations up to 500. Can someone please explain how I can do this? Right now my code is printing all 500 loops.
public class Coin
public static void main(String args)
int heads =0, tails =0, tossResult, total;
double tailsPercent, headsPercent;
Random coinTossing = new Random();
System.out.println("Number of tosses\t\tNo. of Heads\\ % \t\tNo. of Tails\\ %");
for (int i = 1; i <= 50; i++)
tossResult = coinTossing.nextInt(2);
if (tossResult == 0)
total = heads + tails;
headsPercent = ((double)(heads)/total)*100;
tailsPercent = ((double)(tails)/total)*100;
System.out.printf("\t%d\t\t\t\t%d %.1f\t\t%d %.1f", total, heads,
headsPercent, tails, tailsPercent);
If you know a good link that explains this too, please send it my way. Thank you!
Re: Coin Loop Selective Print Statement Problem
Your loop counter is i. You want to do something special for i = 25, i = 50, i = 75, ..., Every value of i that is divisible by 25, ...
In Java, you can find out whether an integer is divisible by another integer by using the "remainder" operator (the '%' sign), which divides the first operand by the second and returns the remainder as its result.
That is to say, for integers a and b:
a is divisible by b if (and only if) a%b is equal to zero.