10 Replies - 783 Views - Last Post: 03 November 2009 - 10:04 AM Rate Topic: -----

#1 chumpster804  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-April 09

for loop

Posted 01 November 2009 - 02:15 PM

i successfully compiled a program:
import java.util.Scanner;

public class xxxx_CaseThree {

public static void main (String[] args) {
   Scanner input = new Scanner (System.in);
   System.out.print ("Enter a 1 to display the number of days in each month in a regular calendar year and a 2 to display the number of days in each month in a leap year:");
   int year = input.nextInt();
   
   while (year < 1 || year > 2) {
	  System.out.println ("Invalid entry. Please try again.");
	  System.out.println ("Enter a 1 to display the number of days in each month in a regular calendar non-leap year and a 2 to display the number of days in each month in a leap year:");
	  year = input.nextInt();
   }  
 
   if (year == 1) {
	  System.out.println ("Month	 Days");
	  System.out.println ("1		  31");
	  System.out.println ("2		  29");
	  System.out.println ("3		  31");
	  System.out.println ("4		  30");
	  System.out.println ("5		  31");
	  System.out.println ("6		  30");
	  System.out.println ("7		  31");
	  System.out.println ("8		  31");
	  System.out.println ("9		  30");
	  System.out.println ("10		 31");
	  System.out.println ("11		 30");
	  System.out.println ("12		 31");
   }
   else {
	  System.out.println ("Month	 Days");
	  System.out.println ("1		  31");
	  System.out.println ("2		  28");
	  System.out.println ("3		  31");
	  System.out.println ("4		  30");
	  System.out.println ("5		  31");
	  System.out.println ("6		  30");
	  System.out.println ("7		  31");
	  System.out.println ("8		  31");
	  System.out.println ("9		  30");
	  System.out.println ("10		 31");
	  System.out.println ("11		 30");
	  System.out.println ("12		 31");
   }

  } // end method main 

} // end class xxxx_CaseThree


my next assignment is to accomplish the same results using an if conditional statement and a for loop. I cannot see the context to use a for loop. Can someone please give me a push start to get through this writers block? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: for loop

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: for loop

Posted 01 November 2009 - 02:22 PM

hi
notice that you have 12 months.
and only 4 posiblities for number of days: 28,29,30,31.
so run the loop 12 times(number of months).
for each iterate, check:
if month equals to 1 or 3 or 5 or 7 or 8 or 10 or 12
print 12 days.
else print 30 days.
make sure you do another check for if month = 2. and according to the year type(1 or 2) print either 29 or 28.

hope it helps :)
Was This Post Helpful? 0
  • +
  • -

#3 chumpster804  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-April 09

Re: for loop

Posted 01 November 2009 - 02:32 PM

I think that makes a little sense. (i'm a little slow). I'll try it. Any more suggestion please feel free.
Was This Post Helpful? 0
  • +
  • -

#4 tivrfoa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 98
  • Joined: 25-January 09

Re: for loop

Posted 01 November 2009 - 02:33 PM

import java.util.Scanner;

public class xxxx_CaseThree {

public static void main (String[] args) {
   Scanner input = new Scanner (System.in);
   System.out.print ("Enter a 1 to display the number of days in each month in a regular calendar year and a 2 to display the number of days in each month in a leap year:");
   int year = input.nextInt();
   
   while (year < 1 || year > 2) {
	  System.out.println ("Invalid entry. Please try again.");
	  System.out.println ("Enter a 1 to display the number of days in each month in a regular calendar non-leap year and a 2 to display the number of days in each month in a leap year:");
	  year = input.nextInt();
   }  
 
	System.out.println ("Month	 Days");

	for(int i=1;i<13;i++) {
		int d = 0;
		if(i==2) {
			if(year == 1) d = 29; else d = 28;
		} else {
			if(i<8) {
				if(i%2==0) d = 30; else d = 31;
			} else {
				if(i%2==0) d = 31; else d = 30;
			}
		}
		System.out.printf("%2d		  %2d\n", i, d);
	}
	 
  } // end method main

} // end class xxxx_CaseThree


Was This Post Helpful? 0
  • +
  • -

#5 chumpster804  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-April 09

Re: for loop

Posted 02 November 2009 - 06:05 PM

ok. i'm a novice java compiler this is my first class so i don't get:
 if(i<8) {
				if(i%2==0) d = 30; else d = 31;
			} else {
				if(i%2==0) d = 31; else d = 30;
			}
		}
		System.out.printf("%2d		  %2d\n", i, d);


i thought % was remainder?
Was This Post Helpful? 0
  • +
  • -

#6 ts230  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 225
  • Joined: 11-July 09

Re: for loop

Posted 02 November 2009 - 06:10 PM

Take a look at this:
http://en.wikipedia.org/wiki/%
Maybe it helps you.
Was This Post Helpful? 0
  • +
  • -

#7 tivrfoa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 98
  • Joined: 25-January 09

Re: for loop

Posted 02 November 2009 - 06:13 PM

View Postchumpster804, on 2 Nov, 2009 - 05:05 PM, said:

i thought % was remainder?
it is
Was This Post Helpful? 0
  • +
  • -

#8 chumpster804  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-April 09

Re: for loop

Posted 03 November 2009 - 07:16 AM

Ok. If someone could please put a little time in this and explain this to me like I'm a 2 yr. old. i = month, d = days? why in
 if(i<8) 
are we seperating Jan. - Aug.? Is it just to have an if else statement? Next, I don't understand
 if(i%2==0) 
. Does this mean even months because of the remainder = 0? Also, how can you have two exact statements
  if(i%2==0) 
result in d = 30 and the next statement d = 31? I have tried to find these answers on my own but for some reason nothing is clicking. Can someone please explain this to me in laymans terms? Thanks for the time.
Was This Post Helpful? 0
  • +
  • -

#9 tivrfoa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 98
  • Joined: 25-January 09

Re: for loop

Posted 03 November 2009 - 07:23 AM

View Postchumpster804, on 3 Nov, 2009 - 06:16 AM, said:

Ok. If someone could please put a little time in this and explain this to me like I'm a 2 yr. old. i = month, d = days? why in
 if(i<8) 
are we seperating Jan. - Aug.? Is it just to have an if else statement? Next, I don't understand
 if(i%2==0) 
. Does this mean even months because of the remainder = 0? Also, how can you have two exact statements
  if(i%2==0) 
result in d = 30 and the next statement d = 31? I have tried to find these answers on my own but for some reason nothing is clicking. Can someone please explain this to me in laymans terms? Thanks for the time.

come on, try to figure out. It's not hard. You're on the right track. =)

i -> month
d -> days

January - July: even months have 30 days (February is an exception)
August - December: even months have 31 days.
Was This Post Helpful? 1
  • +
  • -

#10 chumpster804  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 16-April 09

Re: for loop

Posted 03 November 2009 - 09:43 AM

Thanks you all, but one last explaination and my dumb ass will leave you alone. Please explain:
System.out.printf("%2d		  %2d\n", i, d)


Was This Post Helpful? 0
  • +
  • -

#11 tivrfoa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 98
  • Joined: 25-January 09

Re: for loop

Posted 03 November 2009 - 10:04 AM

View Postchumpster804, on 3 Nov, 2009 - 08:43 AM, said:

Thanks you all, but one last explaination and my dumb ass will leave you alone. Please explain:
System.out.printf("%2d		  %2d\n", i, d)


http://en.wikipedia.org/wiki/Printf
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1