Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Coin Loop Selective Print Statement Problem

  1. #1
    Junior Member
    Join Date
    Jan 2013
    San Francisco
    Thanked 0 Times in 0 Posts

    Unhappy 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.

     package coin;
    import java.util.Random;
    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!

  2. #2
    Join Date
    Jun 2012
    Left Coast, USA
    My Mood
    Thanked 97 Times in 88 Posts

    Default 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.



  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    sternfox (January 30th, 2013)

Similar Threads

  1. Replies: 2
    Last Post: November 15th, 2012, 02:58 PM
  2. help with loop to print an equliateral asterisk triangle
    By everyone0 in forum Loops & Control Statements
    Replies: 13
    Last Post: October 11th, 2012, 12:37 PM
  3. Skipping parts of if loop (Coin toss Program)
    By ChicoTheMan94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2012, 03:52 PM
  4. .indexOf selective replacements
    By msinc210 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 30th, 2012, 04:33 PM
  5. [SOLVED] how to print for-loop output to JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 13th, 2010, 02:43 PM