dcsimg
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
    2,595
    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?
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum 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
    2,595
    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.
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum 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
    2,595
    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.
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum 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
    2,595
    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.
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum 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
    2,595
    Sorry, my bumbling stupidity, it should be more like...

    Code:
    for(int i=number; i>=1; i--)
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum 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



Recent Articles