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: How can I produce this output without using the If statement.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How can I produce this output without using the If statement.

    I have to create a calendar for a single month and I'm starting by trying to figure out how to start a new line after every week. However, I'm not aloud to use if statements which sucks because I know how to use them. I can't think of any other way to do this without using a TON of for loops which is not acceptable for the assignment. We haven't covered while loops either so that's also not an option. Only for loops. I don't need to be given code because I'd rather use my own but any kind of help besides that would be appreciated.

    This is what I have if I use an If statement:
    public class Calendar
    {
       public static void main(String[] args)
       {
          drawDates(31);   
       }
       public static void drawDates(int NumberOfDays)
       {
          int x = 1;
          for (int i = 1; i <= NumberOfDays; i++)
          {
             System.out.print(i + " ");
             x++;
             if (x > 7)
             {
                System.out.println();
                x = 1;
             }   
          }
       }
    }

    Output:
    1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30 31


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I produce this output without using the If statement.

    Look at a nested loop.
    Inner prints days in the current week
    outer prints the newline char
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I produce this output without using the If statement.

    replace conditional statement with the while loop.
    or
    use for(;x>7; ) instead of if(x>7)

Similar Threads

  1. Please Help. Urgent. For loops and printing to output statement.
    By timisaballer23 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 17th, 2013, 08:20 AM
  2. Replies: 1
    Last Post: December 4th, 2012, 07:16 AM
  3. Using a method to add two classes and produce a third
    By shamman84 in forum Object Oriented Programming
    Replies: 6
    Last Post: February 5th, 2012, 09:18 PM
  4. Improving the code to produce the same program
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 13
    Last Post: April 8th, 2011, 09:41 PM
  5. [SOLVED] Listen to two JRadioButton + ons JButton to produce output
    By voltaire in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2010, 03:46 PM