Jump to content

JAVA 12 days of Christmas With 2 switches and 2 for loops

Sergio45
Go to solution Solved by Mr_KoKa,

Ok, so I made a mistake explaining how you can do that in the post before, so I corrected it and now it is ok. You don't use inner loop, just switch, that has cases in reverse order and without any breaks, look at example above, it does it. I cannot edit last post, so there is example without new lines:

switch (i){
  case 3:
    System.out.print('c');
  case 2:
    System.out.print('b');
  case 1:
    System.out.print('a');
}

for i = 1 it will display:

a

 

for i = 2 it will display:

ba

 

for i = 3 it will display:

cba

The issue that I am having is as follows:

I can not get the for loop to go through the switch to display the correct case for the days.

I get the following as my output

**

On the First Day my true love gave to me  A partridge in a pear tree  Turtle Doves  French Hens  Calling Birds  Golden Rings  Geese a laying  Swans a swimming  Maids a milking  Ladies Dancing  Lords a leaping  Pipers piping  Drummers Drumming

**

and it repeats the output through the remaining days.

Thanks in advanced for the help!

 

/* Write as concisely as possible an application that uses repetition and one or more switch
 * statements to print the song 
 * The Twelve days of Christmas
 */
public class twelveday 
{

	public static void main(String [] args)
	{
		
	
		
		
		for(int i=1; i<=12;i++)
	{
		System.out.print("On the ");
	
		switch(i)
		{
		case 1:
			System.out.print("First");
			break;
			
			
		case 2:
			System.out.print("Second");
			break;
				
		case 3:
			System.out.print("Third");
			break;
			
		case 4:
			 System.out.print("Fourth");
			break;
			
		case 5:
			System.out.print("Fifth");
			break;
			
		case 6:
			System.out.print("Sixth");
			break;
		case 7:
			System.out.print("Seventh");
			break;
		case 8:
			System.out.print("Eighth");
			break;
		case 9:
			System.out.print("Ninth");
			break;
		case 10:
			System.out.print("Tenth");
			break;
		case 11:
			System.out.print("Eleventh");
			break;
		case 12:
			System.out.print("Twelfth");
			break;
			
		}
		
		
		System.out.print(" Day my true love gave to me ");
	
	 for (int m=0; m<=12; m++)
	 {
		 switch (m)
			{
			
			case 1:
				System.out.print(" A partridge in a pear tree ");
				break;
			case 2:
				System.out.print(" Turtle Doves ");
				break;
			case 3:
				System.out.print(" French Hens ");
				break;
			case 4:
				System.out.print(" Calling Birds ");
				break;
			case 5:
				System.out.print(" Golden Rings ");
				break;
			case 6:
				System.out.print(" Geese a laying ");
				break;
			case 7:
				System.out.print(" Swans a swimming ");
				break;
			case 8:
				System.out.print(" Maids a milking ");
				break;
			case 9:
				System.out.print(" Ladies Dancing ");
				break;
			case 10:
				System.out.print(" Lords a leaping ");
				break;
			case 11:
				System.out.print(" Pipers piping ");
				break;
			case 12:
				System.out.print(" Drummers Drumming ");
				break;
			}
		 }
	}
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't really know what is mechanic of this song, but I guest it cumulates, right?

 

Your second loop starts from 0, there is no case for m = 0; start it from 1 or change cases indexation.

Your second loop condition must vary, now it always from 0 to 12, make it from 0/1 to i (current index of outer loop) so once outer loop will increase inner loop will print more lines. I think correct inner loop would look like this: 

for (int m=1; m<=i; m++)

You could do this with only one loop and two cases, but gifts would be written in reverse order :P I don't know rules so I don't know if you are interested in it, but you can archive it by creating switch that has cases in reverse order without breaks like this:

switch (i){
  case 3:
    System.out.print('c');
  case 2:
    System.out.print('b');
  case 1:
    System.out.print('a');
}

for i = 1 it will display:

a

 

for i = 2 it will display:

ba

 

for i = 3 it will display:

cba

 

Edit: first switch (this one with numbers) can be replaced by array, but I don't know if your assignment allows for only one switch.

Link to comment
Share on other sites

Link to post
Share on other sites

The song repeats the "objects" of the previous day(s) in the current day verse.

So on the:

On the first day of chirstmas my true love gave to me (a).

on the second day of christmas my true love gave to me (b) and (a)

on the third day of christmas my true love gave to me (c) (b) and (a).

and so on....

 

I do see what you mean about the issue on the inner loop.

Now how would I make it follow that pattern.

a

ba

cba

dcba

and so on?

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, so I made a mistake explaining how you can do that in the post before, so I corrected it and now it is ok. You don't use inner loop, just switch, that has cases in reverse order and without any breaks, look at example above, it does it. I cannot edit last post, so there is example without new lines:

switch (i){
  case 3:
    System.out.print('c');
  case 2:
    System.out.print('b');
  case 1:
    System.out.print('a');
}

for i = 1 it will display:

a

 

for i = 2 it will display:

ba

 

for i = 3 it will display:

cba

Link to comment
Share on other sites

Link to post
Share on other sites

public class twelveday 
{

	public static void main(String [] args)
	{
		
	
		
		
		for(int i=1; i<=12;i++)
	{
		System.out.print("On the ");
	
		switch(i)
		{
		case 1:
			System.out.print("First");
			break;
			
			
		case 2:
			System.out.print("Second");
			break;
				
		case 3:
			System.out.print("Third");
			break;
			
		case 4:
			 System.out.print("Fourth");
			break;
			
		case 5:
			System.out.print("Fifth");
			break;
			
		case 6:
			System.out.print("Sixth");
			break;
		case 7:
			System.out.print("Seventh");
			break;
		case 8:
			System.out.print("Eighth");
			break;
		case 9:
			System.out.print("Ninth");
			break;
		case 10:
			System.out.print("Tenth");
			break;
		case 11:
			System.out.print("Eleventh");
			break;
		case 12:
			System.out.print("Twelfth");
			break;
			
		}
		
		
		System.out.println(" Day of Christmas my true love gave to me ");
		
		for(int m=1; m<=i; m++)
		{
			switch(m)
			{
			case 1:
				System.out.println(" A partridge in a pear tree ");
				break;
			case 2:
				System.out.println(" two Turtle Doves ");
				break;
			case 3:
				System.out.println(" three French Hens ");
				break;
			case 4:
				System.out.println(" four Calling Birds ");
				break;
			case 5:
				System.out.println(" five Golden Rings ");
				break;
			case 6:
				System.out.println(" six Geese a laying ");
				break;
			case 7:
				System.out.println(" seven Swans a swimming ");
				break;
			case 8:
				System.out.println(" eight Maids a milking ");
				break;
			case 9:
				System.out.println(" nine Ladies Dancing ");
				break;
			case 10:
				System.out.println(" ten Lords a leaping ");
				break;
			case 11:
				System.out.println(" eleven Pipers piping ");
				break;
			case 12:
				System.out.println(" twelve Drummers Drumming ");
				break;
			}
		}
		
		
	}
	}
}

Okay, You are saying that for the "for loop" of the prizes(items for the day) do the switches in reverse and take away the breaks? I will try that, but here is what my code looks like so far. 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Mr_KoKa,

 

 I can't get it to go with the pattern. I like your example. I did comment out the inner loop and deleted the breaks from the switch. I did change the switch to do "i" and I get the output in reverse order and as the days goes on the last item doesn't appear. 

 

1st day

1-12

2nd day

2-12

and so on... 

 

/* Write as concisely as possible an application that uses repetition and one or more switch
 * statements to print the song 
 * The Twelve days of Christmas
 */
public class twelveday 
{

	public static void main(String [] args)
	{
		
	
		
		
		for(int i=1; i<=12;i++)
	{
		System.out.print("On the ");
	
		switch(i)
		{
		case 1:
			System.out.print("First");
			break;
			
			
		case 2:
			System.out.print("Second");
			break;
				
		case 3:
			System.out.print("Third");
			break;
			
		case 4:
			 System.out.print("Fourth");
			break;
			
		case 5:
			System.out.print("Fifth");
			break;
			
		case 6:
			System.out.print("Sixth");
			break;
		case 7:
			System.out.print("Seventh");
			break;
		case 8:
			System.out.print("Eighth");
			break;
		case 9:
			System.out.print("Ninth");
			break;
		case 10:
			System.out.print("Tenth");
			break;
		case 11:
			System.out.print("Eleventh");
			break;
		case 12:
			System.out.print("Twelfth");
			break;
			
		}
		
		
		System.out.println(" Day of Christmas my true love gave to me ");
		
		//for(int m=1; m<=i; m++)
		
			switch(i)
			{
			case 1:
				System.out.println(" A partridge in a pear tree ");
				
			case 2:
				System.out.println(" two Turtle Doves ");
				
			case 3:
				System.out.println(" three French Hens ");
				
			case 4:
				System.out.println(" four Calling Birds ");
				
			case 5:
				System.out.println(" five Golden Rings ");
				
			case 6:
				System.out.println(" six Geese a laying ");
				
			case 7:
				System.out.println(" seven Swans a swimming ");
				
			case 8:
				System.out.println(" eight Maids a milking ");
				
			case 9:
				System.out.println(" nine Ladies Dancing ");
				
			case 10:
				System.out.println(" ten Lords a leaping ");
				
			case 11:
				System.out.println(" eleven Pipers piping ");
				
			case 12:
				System.out.println(" twelve Drummers Drumming ");
				
			}
			
			
		}
		
		
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

but you're now printing all prizes on first day, and then one prize less on second, shouldn't it be other way?

My example has cases starting from the highest , so if so, you probably want to start case 12 and go down to case 1 so when it is first day, there will be one prize displayed, and when it will be 6th day 6 prizes will be displayed from 6 to 1 and so on.

Link to comment
Share on other sites

Link to post
Share on other sites

With the help of Mr_KoKa the program is good to go. 

Thanks again Mr_KoKa I know we will be seeing each other in the forums again. I couldn't done it without your guidance. 

What I did was reverse the cases on both switch statements to get the pattern correct!

public class twelveday 
{

	public static void main(String [] args)
	{
		
	
		
		
		for(int i=1; i<=12;i++)
	{
		System.out.print("On the ");
	
		switch(i)
		{
		case 12:
			System.out.print("12th");
			break;
			
			
		case 11:
			System.out.print("11th");
			break;
				
		case 10:
			System.out.print("10th");
			break;
			
		case 9:
			 System.out.print("9th");
			break;
			
		case 8:
			System.out.print("8th");
			break;
			
		case 7:
			System.out.print("7th");
			break;
		case 6:
			System.out.print("6th");
			break;
		case 5:
			System.out.print("5th");
			break;
		case 4:
			System.out.print("4th");
			break;
		case 3:
			System.out.print("3rd");
			break;
		case 2:
			System.out.print("2nd");
			break;
		case 1:
			System.out.print("1st");
			break;
			
		}
		
		
		System.out.println(" Day of Christmas my true love gave to me ");
		
		//for(int m=1; m<=i; m++)
		
			switch(i)
			{
			case 12:
				System.out.println(" twelve Drummers Drumming ");
				
			case 11:
				System.out.println(" eleven Pipers piping ");
				
			case 10:
				System.out.println(" ten Lords a leaping ");
				
			case 9:
				System.out.println(" nine Ladies Dancing ");
				
			case 8:
				System.out.println(" eight Maids a milking ");
				
			case 7:
				System.out.println(" seven Swans a swimming ");
				
			case 6:
				System.out.println(" six Geese a laying ");
				
			case 5:
				System.out.println(" five Golden Rings ");
				
			case 4:
				System.out.println(" four Calling Birds ");
				
			case 3:
				System.out.println(" three French Hens ");
				
			case 2:
				System.out.println(" two Turtle Doves ");
				
			case 1:
				System.out.println(" A partridge in a pear tree ");
				
			}
			
			
		}
		
		
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Glad to help, you didn't have to reordering first switch tough, it wont change anything in this case, cause here are breaks so case 1 always will fire on i = 1 no matter order :P

 

The second case has no break so it going through what is under first matched case to the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×