Results 1 to 3 of 3
  1. #1
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Question about adding three asterisks to every other print line (loops)

    Hi everyone, been a lurker for awhile and I have a question that I'd appreciate help with.

    I'm working on a program which asks to display three asterisks at the end of every 3rd line of output. So far I have the following , but I'm just confused about how to go about this.

    Java Code:
    import java.util.Scanner;
    
    public class LabAssign7
    {
        public static void main(String args[])
        {
    
             // Envokes scanner
             Scanner scan = new Scanner( System.in );
             // Asks user for integer greater than zero
             int n  = 0;
             while (n <= 0) {
             System.out.print( "Enter an integer greater than zero -> " );
             n = scan.nextInt();
                             }
             System.out.print( "\n" );
             //Declares loop variable
             int i;
             // Calculates text to display set number of times
             for ( i = 0; i < n ; i++)
             {    
             System.out.print( "I like to sit by the fireplace.\n" );
             
        }
        System.out.print( "\nThe program has ended.\n" );
          
          
        
        }
    }
    Output:
    Java Code:
     Enter an integer greater than zero -> -9
    Enter an integer greater than zero -> 9
    
    I like to sit by the fireplace.
    I like to sit by the fireplace.
    I like to sit by the fireplace.
    I like to sit by the fireplace.
    I like to sit by the fireplace.
    I like to sit by the fireplace.
    I like to sit by the fireplace.
    I like to sit by the fireplace.
    I like to sit by the fireplace.
    
    The program has ended.
    Any hints / comments would be appreciated. Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Question about adding three asterisks to every other print line (loops)

    The modulus operator (%) is useful to detecting every nth value in a sequence of numbers.
    0%4 is 0
    1%4 is 1
    2%4 is 2
    3%4 is 3
    4%4 is 0
    5%4 is 1
    6%4 is 2
    7%4 is 3
    8%4 is 0
    9%4 is 1
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Question about adding three asterisks to every other print line (loops)

    thanks ! i got it !

Similar Threads

  1. Where does he print this certain line?
    By Lund01 in forum Advanced Java
    Replies: 6
    Last Post: 11-18-2010, 02:23 PM
  2. print every other line
    By welikedogs in forum New To Java
    Replies: 23
    Last Post: 11-16-2010, 05:40 PM
  3. print to the next line
    By bar in forum New To Java
    Replies: 14
    Last Post: 07-15-2010, 01:48 AM
  4. Print to next Line
    By singularity in forum New To Java
    Replies: 13
    Last Post: 09-10-2009, 09:03 AM
  5. print line
    By kazitula in forum Java Applets
    Replies: 2
    Last Post: 01-26-2008, 02:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •