www.webdeveloper.com
Results 1 to 9 of 9

Thread: 12 days of christmas please help!

  1. #1
    Join Date
    Jan 2014
    Posts
    5

    12 days of christmas please help!

    Hello I am currently on a break from University I need help with the last part of this work I really cant wrap my head around it. Basically I need to change the switch into ifs so that it still works, here is the code I have with the switch:

    public class christmas {

    public static void main(String[] args) {
    int number;
    System.out.print("Enter number: ");
    //number=readInt();
    number = 1;
    System.out.println();

    for(int i=1; i<=number; i++) {
    System.out.print("On the ");
    switch(i) {
    case 1: System.out.print("1st"); break;
    case 2: System.out.print("2nd"); break;
    case 3: System.out.print("3rd"); break;
    default: System.out.print(i+"th");
    }
    System.out.println(" day of Christmas my true love gave to me");
    switch(i) {
    case 12: System.out.print("twelve drummers drumming, ");
    case 11: System.out.println("eleven pipers piping, ");
    case 10: System.out.print("ten lords a-leping, ");
    case 9: System.out.print("nine ladies dancing, ");
    case 8: System.out.println("eight maids a-milking, ");
    case 7: System.out.print("seven swans a-swimming, ");
    case 6: System.out.print("six geese a-laying, ");
    case 5: System.out.println("five gold rings,");
    case 4: System.out.print("four calling birds, ");
    case 3: System.out.print("three french hens, ");
    case 2: System.out.print("two turtle doves\nand ");
    case 1: System.out.println("a partridge in a pear tree.");
    }
    System.out.println();

    }}}
    Please help.

  2. #2
    Join Date
    Jan 2014
    Posts
    5
    Please help I need to change the switch to a if or else. I need help fast with this please.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,859
    This is not JavaScript... However, as they are similar in construct...

    if( i==1 ){System.out.println("a partridge in a pear tree.");}
    if( i==2) ...

    etc.

    Would that be what you are looking for?
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Jan 2014
    Posts
    5
    Quote Originally Posted by \\.\ View Post
    This is not JavaScript... However, as they are similar in construct...

    if( i==1 ){System.out.println("a partridge in a pear tree.");}
    if( i==2) ...

    etc.

    Would that be what you are looking for?
    Yes but I have just changed it all and now nothing works at all? I also posted this to javascript and they told me to go to Java so I am unsure where exactly I am to post this if both sides tell me to go to the other.


    public class christmas {

    public static void main(String[] args) {
    int number;
    System.out.print("Enter number: ");
    //number=readInt();
    number =12;
    System.out.println();

    for(int i=1; i<=number; i++) {
    System.out.print("On the ");

    if(i==1){System.out.print("1st"); break;}
    if(i==2){ System.out.print("2nd"); break;}
    if(i==3){System.out.print("3rd"); break;}
    else {System.out.print(i+"th");}
    }
    System.out.println(" day of Christmas my true love gave to me");


    if(i==12)System.out.print("twelve drummers drumming, ");
    if(i==11)System.out.println("eleven pipers piping, ");
    if(i==10)System.out.print("ten lords a-leping, ");
    if(i==9)System.out.print("nine ladies dancing, ");
    if(i==8)System.out.println("eight maids a-milking, ");
    if(i==7)System.out.print("seven swans a-swimming, ");
    if(i==6)System.out.print("six geese a-laying, ");
    if(i==5)System.out.println("five gold rings,");
    if(i==4)System.out.print("four calling birds, ");
    if(i==3)System.out.print("three french hens, ");
    if(i==2)System.out.print("two turtle doves\nand ");
    if(i==1)System.out.println("a partridge in a pear tree.");

    }}

    That is what I have at the moment it doesn't work at all I have to have that working so that where you see number = you can put a number between 1 and 12 and it wall say the christmas song that represents that number so 1 would be "on the first day of christmas my true love gave to me a partridge in a pear tree" please help.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,859
    What are you using as an editor? Does it not give you a console that reports the errors?

    If Java is anything like C then a minor typo will cause the program to not work at all.

    If this was a JavaScript program then the following would be how I would do things.

    HTML Code:
    <form id="form1" name="displayout" method="post" action="javascript:;">
      <textarea name="carol" id="carol" cols="80" rows="8"></textarea>
    </form>
    <script>
    //12 days of christmas  
    gift = ["",
    		"a partridge in a pear tree.",
    		"two turtle doves ",
    		"three french hens, ",
    		"four calling birds, ",
    		"five gold rings,",
    		"six geese a-laying, ",
    		"seven swans a-swimming, ",
    		"eight maids a-milking, ",
    		"nine ladies dancing, ",
    		"ten lords a-leaping, ",
    		"eleven pipers piping, ",
    		"twelve drummers drumming, "
    		]; 
    
    stdout = document.displayout.carol;
    
    while( number = prompt("Enter a number 1 to 12","") ){
    	first = ( number==1 ) ? true : false;
    	if( number>0 && number<=12 ){
    		var str = "On the " + number;
    			switch( number ) { 
    				case 1: str += "st"; break; 
    				case 2: str += "nd"; break; 
    				case 3: str += "rd"; break; 
    				default: str += "th";
    			}
    		
    			str += " day of Christmas my true love gave to me ";
    			
    			
    		while( number > 1 ){
    			str += gift[ number-- ];
    			str += "\r\n";
    		}
    		
    		str += (first ? " " : "and ") + gift[1];
    		str += "\r\n";
    	
    	stdout.value += str;
    	
    	}
    }
    </script>
    how that would translate in to Java is anybody's guess.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  6. #6
    Join Date
    Jan 2014
    Posts
    5
    Quote Originally Posted by \\.\ View Post
    What are you using as an editor? Does it not give you a console that reports the errors?

    If Java is anything like C then a minor typo will cause the program to not work at all.

    If this was a JavaScript program then the following would be how I would do things.

    HTML Code:
    <form id="form1" name="displayout" method="post" action="javascript:;">
      <textarea name="carol" id="carol" cols="80" rows="8"></textarea>
    </form>
    <script>
    //12 days of christmas  
    gift = ["",
    		"a partridge in a pear tree.",
    		"two turtle doves ",
    		"three french hens, ",
    		"four calling birds, ",
    		"five gold rings,",
    		"six geese a-laying, ",
    		"seven swans a-swimming, ",
    		"eight maids a-milking, ",
    		"nine ladies dancing, ",
    		"ten lords a-leaping, ",
    		"eleven pipers piping, ",
    		"twelve drummers drumming, "
    		]; 
    
    stdout = document.displayout.carol;
    
    while( number = prompt("Enter a number 1 to 12","") ){
    	first = ( number==1 ) ? true : false;
    	if( number>0 && number<=12 ){
    		var str = "On the " + number;
    			switch( number ) { 
    				case 1: str += "st"; break; 
    				case 2: str += "nd"; break; 
    				case 3: str += "rd"; break; 
    				default: str += "th";
    			}
    		
    			str += " day of Christmas my true love gave to me ";
    			
    			
    		while( number > 1 ){
    			str += gift[ number-- ];
    			str += "\r\n";
    		}
    		
    		str += (first ? " " : "and ") + gift[1];
    		str += "\r\n";
    	
    	stdout.value += str;
    	
    	}
    }
    </script>
    how that would translate in to Java is anybody's guess.
    I have to use eclipse if you can help me with this I would be so thankful. Eclipse helios.

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,859
    Just had a look, no way I can afford to download it to check it out as I am low on data allowance and have to conserve my time on just browsing.

    I suggest that you investigate your development interface to get a picture of where the error is.

    I am not familiar with the use of Java and how it is constructed but I am familiar with JavaScript hence my rendering as above, it should help you understand the process and the logic that I have applied to your problem.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,859
    Having looked in to you code, I find this highly suspect

    Code:
    for(int i=1; i<=number; i++)
    if you are recursing then you wouldn't be i++ would you? I would say that you loop should be...

    Code:
    for(int i=number; i>=number; i--)
    See if that helps.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,859
    Sorry, my bumbling stupidity, it should be more like...

    Code:
    for(int i=number; i>=1; i--)
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.16975 seconds
  • Memory Usage 2,948KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (2)bbcode_html
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates