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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: Need Help! For loops and if/else

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry Need Help! For loops and if/else

    I don't see what is wrong i wrote it down on paper but here is the task i am trying to accomplish.

    Print out a count-by-count matrix of asterisks and percent signs, alternating by row
    Example:
    if count = 5
    output will be:

    * * * * *
    % % % % %
    * * * * *
    % % % % %
    * * * * *
    My Code:

    int t = 1;
    int k = 2;
    for(int i=1; i<=count; i++){ // how many sets of for j(5) COUNT IS USER INPUT but it is not in this code
    if(i == t){
    for(int j=1; j<=count; j++){ //how many asterisks get printed to a line(5)
    System.out.print(ast);
    t+=2; // prints on odd rows
    }
    }
    else if(i == k){ // checks when j is = to count(t)
    for(int l=1; l <= count; l++){
    System.out.print('%');
    k*=2; // prints only on even rows
    }
    }
    System.out.println();
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Need Help! For loops and if/else

    All you need is 1 for loop and a conditional statement.

    for (index to count)
    if index is an odd number, print stars
    else print percentage
    end

    To determine if a number is even or odd, use the remainder operator (resources found here)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    kram (February 11th, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help! For loops and if/else

    Thank You sir, why do i make things so complicated.

Similar Threads

  1. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  2. help with loops
    By kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2011, 11:42 PM
  3. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  4. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM
  5. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM