5 Replies - 262 Views - Last Post: 04 March 2013 - 02:43 AM Rate Topic: -----

#1 Nightryno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-March 13

Switch with incorrect output

Posted 01 March 2013 - 09:29 AM

Hello: What I am trying to do is create a program that will output the 12 days of Christmas song where each verse adds one line. I have to use a switch statement in a loop to control which lines get printed and avoid the break statement. Additionally, I have to use a separate switch statement to put the appropriate suffix on the day number (1st, 2nd, 3rd, etc.).

My problem is for my output the first three days are giving me the incorrect output. I get:

On the 1stndrdth day of Christmas
On the 2ndrdth day of Christmas
On the 3rdth day of Christmas

And it should obviously only be: 1st, 2nd, 3rd. Maybe it's staring at the screen too long but I can't see my misstep. Can somebody provide some guidance as to what I have structured incorrectly? Thank you.




/**
* This class outputs the 12 days of christmas song
*
* @author
* @version
*/
public class ChristmasSong
{
   public static void main ( String [] args )
   {
       String result = "";
       
       for ( int day = 1; day <= 12; day++ )
       {
           result += "\nOn the " + day;
           
           switch ( day ) 
           {
               case 1:
               
               result += "st";
                              
               case 2:
               
               result += "nd";
                              
               case 3:
               
               result += "rd";
                              
               default:
               
               result += "th";
                           }
            
            result += " day of Christmas, my true love gave to me: ";
            
            switch ( day )
            {
                case 12:
                
                result += " Twelve drummers drumming, ";
                
                case 11:
                    result += " Eleven pipers piping, ";
                    
                case 10:
                    result += " Ten lords-a-leaping, ";
                    
                case 9:
                    result += " Nine ladies dancing, ";
                    
                case 8:
                    result += " Eight maids-a-milking, ";
                    
                case 7:
                    result += " Seven swans-a-swimming, ";
                    
                case 6:
                    result += " Six geese-a-laying, ";
                    
                case 5:
                    result += " Five golden rings.";
                    
                case 4:
                    result += " Four calling birds, ";
                    
                case 3:
                    result += " Three French hens, ";
                    
                case 2:
                    result += " Two turle doves, and ";
                    
                case 1:
                    result += " a Patridge in a pear tree.";
                }
            }
            System.out.println ( result );
                
    
    }
    
}




Is This A Good Question/Topic? 0
  • +

Replies To: Switch with incorrect output

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: Switch with incorrect output

Posted 01 March 2013 - 09:36 AM

Quote

and avoid the break statement.

I would guess 'they' mean avoid it in the loop and NOT in switch, where it's usually necessary in the absence of a return statement. Look into that issue as the source of errors
Was This Post Helpful? 1
  • +
  • -

#3 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Switch with incorrect output

Posted 01 March 2013 - 09:38 AM

Without a break in your switch statement your switch statement will fall through. You probably just need to avoid break statements in your loops. If you absolutely wanna avoid the break statement you can use a method that returns a String.

public String methodName(int day) {
  switch(day){
   case 1: return "st";
   //and so on
  }
}


This post has been edited by Kinaces: 01 March 2013 - 09:43 AM

Was This Post Helpful? 1
  • +
  • -

#4 Nightryno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-March 13

Re: Switch with incorrect output

Posted 01 March 2013 - 07:28 PM

Makes sense. I was just confused as to the not using the break requirement. I appreciate the guidance
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Switch with incorrect output

Posted 04 March 2013 - 01:44 AM

Well, you only need to use the break statements in your first switch control, and not for the second one...

 switch ( day ) 
           {
               case 1:
               
               result += "st";
               break;
                              
               case 2:
               
               result += "nd";
               break;
                              
               case 3:
               
               result += "rd";
               break;               
               default:
               
               result += "th";
           }

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: Switch with incorrect output

Posted 04 March 2013 - 02:43 AM

Of course, the second part is much more elegantly solved with an array.

There's a tiny bug in the 1st day - take a look at it
No there isn't ;)

This post has been edited by g00se: 04 March 2013 - 02:44 AM
Reason for edit:: error

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1