/    Sign up×
Community /Pin to ProfileBookmark

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 dovesnand “);
case 1: System.out.println(“a partridge in a pear tree.”);
}
System.out.println();

}}}
Please help.

to post a comment
Java

8 Comments(s)

Copy linkTweet thisAlerts:
@ta123123authorJan 08.2014 — Please help I need to change the switch to a if or else. I need help fast with this please.
Copy linkTweet thisAlerts:
@rootJan 08.2014 — 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?
Copy linkTweet thisAlerts:
@ta123123authorJan 09.2014 — 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?[/QUOTE]


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 dovesnand ");
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.
Copy linkTweet thisAlerts:
@rootJan 09.2014 — 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.

[code=html]<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 += "rn";
}

str += (first ? " " : "and ") + gift[1];
str += "rn";

stdout.value += str;

}
}
</script>[/code]


how that would translate in to Java is anybody's guess.
Copy linkTweet thisAlerts:
@ta123123authorJan 09.2014 — 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.

[code=html]<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 += "rn";
}

str += (first ? " " : "and ") + gift[1];
str += "rn";

stdout.value += str;

}
}
</script>[/code]


how that would translate in to Java is anybody's guess.[/QUOTE]


I have to use eclipse if you can help me with this I would be so thankful. Eclipse helios.
Copy linkTweet thisAlerts:
@rootJan 10.2014 — 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.
Copy linkTweet thisAlerts:
@rootJan 10.2014 — Having looked in to you code, I find this highly suspect

[CODE]for(int i=1; i<=number; i++)[/CODE]

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--)[/CODE]

See if that helps.
Copy linkTweet thisAlerts:
@rootJan 10.2014 — Sorry, my bumbling stupidity, it should be more like...

[CODE]for(int i=number; i>=1; i--)[/CODE]
×

Success!

Help @ta123123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...