The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Rearrange switch statements and put break

Posted by Kishori Sharan on February 19, 2002 at 2:21 PM

I think this is what you want. First swap the two switch statements and then put a break statement for each case only in first switch.
//////////////////////////////////////////////
import java.io.*;

public class ChristmasSong {
public static void main (String[] args) {
//int verse = (1-12);

for ( int verse = 1 ; verse <= 12 ; verse++ ) {
switch ( verse + 12){
case 24: System.out.println ("On the 12th day of Christmas my true love gave to me: "); break ;
case 23: System.out.println ("On the 11th day of Christmas my true love gave to me: "); break ;
case 22: System.out.println ("On the 10th day of Christmas my true love gave to me: "); break ;
case 21: System.out.println ("On the 9th day of Christmas my true love gave to me: "); break ;
case 20: System.out.println ("On the 8th day of Christams my true love gave to me: "); break ;
case 19: System.out.println ("On the 7th day of Christmas my true love gave to me: "); break ;
case 18: System.out.println ("On the 6th day of Christmas my true love gave to me: "); break ;
case 17: System.out.println ("On the 5th day of Christmas my true love gave to me: "); break ;
case 16: System.out.println ("On the 4th day of Christmans my true love gave to me: "); break ;
case 15: System.out.println ("On the 3rd day of Christmas my true love gave to me: "); break ;
case 14: System.out.println ("On the 2nd day of Christmas my true love gave to me: "); break ;
case 13: System.out.println ("On the 1st day of Christmas my true love gave to me: "); break ;
}
switch (verse) {
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, and");
case 1: System.out.println ("A partridge in a pear tree");
}
System.out.println ( "\n\n" ) ;

}
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us